본문 바로가기

00/algorithm

(55)
Container With Most Water (in Python) Problem Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, O). Find two lines, which together with x-axis forms a container, such that the container contains the most water. Note : You may not slant the container and n is at least 2. The above vertical lines are..
ZigZag Conversion (in Python) Problem The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this :P A H N A P L S I I G Y I RAnd then read line by line : "PAHNAPLSIIGYIR" Solution Approach 1 : Sort by Row IntuitionBy iterating through the string from left to right, we can easily determine which row in the Zig-Zag pattern that a character belongs to. Algorithm1. We can use min(numRows, len(..
Longest Palindromic Substring (in Python) Problem Given a string s, find the longest palindromic substring in s. you may assume that the maximum length of s is 1000. Solution Make sure you understand what a palindrome means. A palindrome is a string which reads the same in both directions. For example, S="aba" is a palindrome, S="abc" is not. Approach 1 : Brute Force The obvious brute force solution is to pick all possible starting and ..
Median of Two Sorted Arrays (in Python) Problem There are two sorted arrays num1 and num2 of size m and n respectively.Find the median of the two sorted arrays. The overall run time complexity should be O(log(m+n)). You may assume nums1 and nums2 cannot be both empty. Solution Approach 1 : Sort and find My first approach was just put them together and find median. It costs same time with the second approach on Leetcode. But it will wa..
What does python's sort use? The answer is Timsort. Definition•A hybrid stable sorting algorithm, derived from merge sort and insertion sort.•Designed to perform well on many kinds of real-world data.•It was implemented by Tim Peters in 2002 for use in the Python programming language.•The algorithm finds subsequences of the data that are already ordered, and uses that knowledge to sort the remainder more efficiently.•This i..
Longest Substring Without Repeating Characters (in Python) Problem Given a string, find the length of the longest substring without repeating characters. Solution Approach 1 : Brute Force IntuitionCheck all the substring one by one to see if it has no duplicate character. Algorithm def lengthOfLongestSubstring(self, s): """ :type s: str :rtype: int """ if len(s) == 0 : return 0 result = 1 for i in range(len(s)) : for j in range(i+1, len(s)) : if s[j] in..
Add two numbers (in Python) Problem You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself. Solution Approach 1 : Elementary Math Intuition Keep track of the carry using ..
Two Sum (in Python) Problem Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution, and you may not use the same element twice. Solution Approach 1 : Brute Force The brute force approach is simple.Loop through each element x and find if there is another value that equals to target - x. def twoSum(nums, t..