Home LeetCode Practice
Post
Cancel

LeetCode Practice

LeetCode

My LeetCode practice notes

#ProblemDifficultyTagsNotes
1TwoSumEasyArray Hash Table Two PointersBrute Force $\mathcal O(n^2)$

Two Pointers $\mathcal O(n\log n)$: Sort and set two pointer at left and right of the list, increase left if sum < target and decrease right if sum > target.

Two-pass Hash Table $\mathcal O(n)$: Build a {number: index} table, for each element look for target - element in the table.

One-pass Hash Table $\mathcal O(n)$: Create the table on the fly.
2ThreeSumMediumArray Hash Table Two PointersTwo Pointers $\mathcal O (n^2)$: Sort and iterate the list for target, use two poiters approach to search in the rest of the list.

Hash Table $\mathcal O (n^2)$: Sort and build a {number: index} table, iterate the list for two numbers and search the third number in the table.
3ThreeSum ClosestMediumArray Two PointersTwo Pointers $\mathcal O (n^2)$: Same as Two Pointers solution for ThreeSum problem but keep track of best distance and best sum.
4Longest Common PrefixEasyStringHorizontal $\mathcal O (c)$: $lcp(s_1, s_2, s_3) = lcp(lcp(s_1, s_2), s_3)$

Vertical $\mathcal O (c)$: Find the shortest string and match its characters, one by one, with the rest of the strings.
5Longest Substring without Repeating CharactersMediumString Window Hash TableWindow $\mathcal O (n)$: Add unseen characters to a {char: index} table until you find a character that you have seen. If the seen character is outside of current window keep going else move the left pointer to the right of the previously seen character.
6Longest Palindromic SubstringMediumString DPDP $\mathcal O (n^2)$: Substring s[i:j+1] is palindrome if s[i]==s[j] and s[i+1:j] is palindrome.
7Valid ParenthesesEasyStackStack $\mathcal O (n)$
8Generate ParenthesesMediumBacktracking DPBacktracking: Assume a binary tree where edges are ( and ) and nodes are sequence of parentheses from root to that node (root is empty). Traverse the tree (depth first) to depth of n and check the validity of the parentheses. If not valid backtrack else if you have n pairs of parentheses add the result and backtrack else continue traversing.

Dynamic Programming: Each element in the set of n-pair valid parentheses, dp[n] (n > 1), can be broken into ( + *dp[i] + ) + *dp[n-i-1] for i=0,...,n-1 where *dp[i] is all the emelents of dp[i].
9Container with Most WaterMediumArray Two Pointers GreedyTwo Pointers $\mathcal O (n)$:
10Reverse IntegerMediumMath$\mathcal O(\log_{10} n)$
<!– 11Jump Game –>   
This post is licensed under CC BY 4.0 by the author.