|
44 | 44 | | 109 | [Convert Sorted List to Binary Search Tree](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/109_Convert_Sorted_List_to_Binary_Search_Tree.py) | 1. Two points fast (next next) and slow (next) O(nlgn) and O(n)<br>2. Bottom-up recursion O(n) and O(lgn) |
|
45 | 45 | | 110 | [Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/110_Balanced_Binary_Tree.py) | Recursion 1. Top-down O(n^2) and O(n), Bottom-up recursion with sentinel -1 O(n) and O(n) |
|
46 | 46 | | 111 | [Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/111_Minimum_Depth_of_Binary_Tree.py) | 1. Recursion, note that when size of left (ld) or right (rd) is 0, then min = 1 + ld + rd<br> 2. BFS check by level (right most), which is much faster than recursion |
|
47 |
| -| 124 | [Binary Tree Maximum Path Sum](https://leetcode.com/problems/binary-tree-maximum-path-sum/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/124. Binary Tree Maximum Path Sum.py) | Recursion O(n) and O(n), max (left + node, right + node, left + node + right) | |
| 47 | +| 124 | [Binary Tree Maximum Path Sum](https://leetcode.com/problems/binary-tree-maximum-path-sum/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/124_Binary_Tree_Maximum_Path_Sum.py) | Recursion O(n) and O(n), max (left + node, right + node, left + node + right) | |
48 | 48 | | 125 | [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/125_Valid_Palindrome.py)| Exclude non-alphanumeric characters and compare O(n) |
|
49 | 49 | | 128 | [Longest Consecutive Sequence](https://leetcode.com/problems/longest-consecutive-sequence/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/128_Longest_Consecutive_Sequence.py) | Set or hash, pop adjacency, O(n) and O(n) |
|
50 | 50 | | 133 | [Clone Graph](https://leetcode.com/problems/clone-graph/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/133_Clone_Graph.py) | Hash and DFS or BFS |
|
|
71 | 71 | | 163 | [Missing Ranges](https://leetcode.com/problems/missing-ranges/) ♥ | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/163_Missing_Ranges.py) | Add -1 to lower for special case, then check if curr - prev >= 2|
|
72 | 72 | | 166 | [Fraction to Recurring Decimal](https://leetcode.com/problems/fraction-to-recurring-decimal/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/166_Fraction_to_Recurring_Decimal.py) | % and Hash to find duplicate |
|
73 | 73 | | 167 | [Two Sum II - Input array is sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/167_Two_Sum_II_Input_array_is_sorted.py) | Two points O(n) and O(1) |
|
74 |
| -| 170 | [Two Sum III - Data structure design](https://leetcode.com/problems/two-sum-iii-data-structure-design/) |
75 |
| -♥ | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/170_Two_Sum_III-Data_structure_design.py) | 1. Hash, O(1) for add, O(n) for find, O(n) space<br>2. sorted list, O(logn) for add, O(n) for find, O(n) space<br> 3. Sort before find, O(1) for add, O(nlogn) for find, O(n) space| |
| 74 | +| 170 | [Two Sum III - Data structure design](https://leetcode.com/problems/two-sum-iii-data-structure-design/) ♥ | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/170_Two_Sum_III-Data_structure_design.py) | 1. Hash, O(1) for add, O(n) for find, O(n) space<br>2. sorted list, O(logn) for add, O(n) for find, O(n) space<br> 3. Sort before find, O(1) for add, O(nlogn) for find, O(n) space| |
76 | 75 | | 186 | [Reverse Words in a String II](https://leetcode.com/problems/reverse-words-in-a-string-ii/) ♥| [Python](https://github.com/qiyuangong/leetcode/blob/master/python/186_Reverse_Words_in_a_String_II.py) | Reverse all and reverse each words |
|
77 | 76 | | 198 | [House Robber](https://leetcode.com/problems/house-robber/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/198_House_Robber.py) | f(k) = max(f(k – 2) + num[k], f(k – 1)), O(n) and O(1) |
|
78 | 77 | | 200 | [Number of Islands](https://leetcode.com/problems/number-of-islands/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/200_Number_of_Islands.py) | 1. Quick union find, O(nlogn) and O(n^2)<br>2. BFS with marks, O(n^2) and O(1) |
|
|
93 | 92 | | 270 | [Closest Binary Search Tree Value](https://leetcode.com/problems/closest-binary-search-tree-value/) ♥ | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/270_Closest_Binary_Search_Tree_Value.py) | 1. Recursively brute force, O(n) and O(n)<br>2. Recursively compare root result with current kid's result (left or right), O(logn) and O(logn)<br>3. Iteratively compare root result with current kid's result (left or right), O(logn) and O(logn) |
|
94 | 93 | | 274 | [H-Index](https://leetcode.com/problems/h-index/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/274_H-Index.py) | [Background](https://en.wikipedia.org/wiki/H-index)<br>1. Sort and check number of papers greater than h, O(nlogn) and O(1)<br>2. Counting sort, O(n) and O(n) |
|
95 | 94 | | 276 | [Paint Fence](https://leetcode.com/problems/paint-fence/) ♥ | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/276_Paint_Fence.py) | ways[i>2] = (ways[i-1] + ways[i-2]) * (k - 1), O(n) and O(1) |
|
96 |
| -| 280 | [Wiggle Sort](https://leetcode.com/problems/wiggle-sort/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/280_Wiggle_Sort.py) ♥ | 1. Sort and insert (n - 1) / 2 from tail to correct position, O(nlogn) and O(1)<br>2. Sort and swap(i, i + 1) from 1 to n - 1, O(nlogn)<br>3. Iteratively check order and reverse order, if not satisfied, then swap i with i + 1, O(n) | |
| 95 | +| 280 | [Wiggle Sort](https://leetcode.com/problems/wiggle-sort/) ♥ | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/280_Wiggle_Sort.py) | 1. Sort and insert (n - 1) / 2 from tail to correct position, O(nlogn) and O(1)<br>2. Sort and swap(i, i + 1) from 1 to n - 1, O(nlogn)<br>3. Iteratively check order and reverse order, if not satisfied, then swap i with i + 1, O(n) | |
97 | 96 | | 286 | [Walls and Gates](https://leetcode.com/problems/walls-and-gates/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/286_Walls_and_Gates.py) | BFS with queue, O(mn) and O(mn) |
|
98 | 97 | | 288 | [Unique Word Abbreviation](https://leetcode.com/problems/unique-word-abbreviation/) ♥ | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/288_Unique_Word_Abbreviation.py) | hash |
|
99 | 98 | | 293 | [Flip Game](https://leetcode.com/problems/flip-game/) ♥ | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/293_Flip_Game.py) | Python string slicing |
|
|
105 | 104 | | 337 | [House Robber III](https://leetcode.com/problems/house-robber-iii/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/337_House_Robber_III.py) | 1. Recursion with hash map, O(n) and O(n)<br>2. Recursion on two steps, O(n) and O(1) |
|
106 | 105 | | 339 | [Nested List Weight Sum](https://leetcode.com/problems/nested-list-weight-sum/) ♥ | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/339_Nested_List_Weight_Sum.py) | Depth-first recursion |
|
107 | 106 | | 340 | [Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/) ♥ | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/340_Longest_Substring_with_At_Most_K_Distinct_Characters.py) | Maintain a sliding window with at most k distinct characters and a count for this window. Note that the start position need a loop to update.|
|
108 |
| -| 346 | [Moving Average from Data Stream](https://leetcode.com/problems/moving-average-from-data-stream/) ♥ | |
109 |
| -[Python](https://github.com/qiyuangong/leetcode/blob/master/python/346_Moving_Average_from_Data_Stream.py) | fix-sized queue or dequeue, O(1) and O(n) | |
| 107 | +| 346 | [Moving Average from Data Stream](https://leetcode.com/problems/moving-average-from-data-stream/) ♥ | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/346_Moving_Average_from_Data_Stream.py) | fix-sized queue or dequeue, O(1) and O(n) | |
110 | 108 | | 351 | [Android Unlock Patterns](https://leetcode.com/problems/android-unlock-patterns/) ♥ | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/351_Android_Unlock_Patterns.py) | Backtracking, O(n!) and O(n) |
|
111 | 109 | | 359 | [Logger Rate Limiter](https://leetcode.com/problems/logger-rate-limiter/) ♥ | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/359_Logger_Rate_Limiter.py) | 1. hash which stores the latest timestamp, O(1) and O(n)<br>2. Using Priority queue to remove older logs, O(n) and O(n) |
|
112 | 110 | | 366 | [Find Leaves of Binary Tree](https://leetcode.com/problems/find-leaves-of-binary-tree/) ♥ | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/366_Find_Leaves_of_Binary_Tree.py) | 1. Set or hash to check leaft, O(n^2) and O(n)<br>2. Recursively check level and return them, O(n) and O(n)|
|
|
0 commit comments