DP
- 70. Climbing Stairs
- 746. Min Cost Climbing Stairs
- 1137. N-th Tribonacci Number
- nums[i] = nums[i-1] + nums[i-2] + nums[i-3]
- 338. Counting Bits
- p(x+b) = p(x) + 1, b = 2^m
- 118. Pascal’s Triangle
- res = [[1] * i for i in range(1, numRows + 1)]
- res[i][j] = res[i-1][j-1] + res[i-1][j]
Two Pointers
Problem Title | Status | notes |
---|---|---|
167. Two Sum II - Input Array Is Sorted | ✅ | left & right pointer |
11.Container With Most Water | ✅ | left & right pointer |
15. 3 Sum | ✅ Solution | left & right pointer |
16. 3 Sum Closest | ||
18. 4 Sum | ✅ Solution | |
19. Remove Nth Node From End of List | ||
26. Remove Duplicates From Sorted Array | Given a sorted array, remove the duplicates in the array such that each element appears only once. ✅ | similar problem 82. Remove Duplicates from Sorted List II 83. Remove Duplicates from Sorted List 1836. Remove Duplicates From an Unsorted Linked List |
28. Implement strStr() | Return the index of the first occurrence of substring in a string ✅ | KMP will be better |
42. Traping Rain Watter | ✅ | Left & right pointer, Need more practice 11. Container With Most Water |
75. Sort Colors | ✅Solution | Left & Right pointer |
80. Remove Duplicates from Sorted Array II | Given a sorted array, remove the duplicates in the array such that each unique element appears at most twice ✅ | |
125. Valid Palindrome | ✅ Useful Python string methods: s.lower() s.isalpha() s.isnumeric() s.isalnum() | Left & right pointer. |
345. Reverse Vowels in String | ✅ | Left & right pointer s is not changable, we should us s = list(s) |
532. K-diff Pairs in an Array | Given an array of integers nums and an integer k, return the number of unique k-diff pairs in the array. ✅ | fast & slow pointer |
557. Reverse Words in a String III | ✅ | fast & slow pointer, s[slow:fast] = reversed(s[slow:fast]) |
Leetcode 905. Sort Array By Parity | ✅ | fast & slow pointer |
Leetcode 922. Sort Array By Parity II | ✅ | even & odd pointer |
Leetcode 977 Squares of a Sorted Array | ✅ Solution | left & right pointer. |
- 532. K-diff Pairs in an Array (fast and slow pointer)
Linked List
Math Related Problems
ProblemTitle | Content | Status | notes |
---|---|---|---|
258. Add Digits | Given an integer num, repeatedly add all its digits until the result has only one digit, and return it. | ✅ | $nums = d_0 + d_1*10 + … + d_k * 10 ^ k$ $ = (d_0 + d_1 + … + d_k) + 9 * m$ $(d_0 + d_1 + … + d_k) \% 9 = nums \% 9$ |
50. Pow(x, n) | Solution | ✅ | Divide and Conquer |
191. Number of 1 Bits | Solution | similar problems: https://leetcode.com/problems/power-of-two/ | Bit manipulation: n&(n-1) |
190. Reverse Bits | Bit manipulation: n & 1 | ans « 1 |
String Problems
Problem Title | Status | Notes |
---|---|---|
14. Longest Common Prefix | ✅ | LCP(S1…Sn) = LCP(LCP(LCP(S1,S2), S3)… Sn) |
151. Reverse Words in a String | ✅ | ” “.join(reversed(s.split())) |
Sweep Line
Diff Array
diff[i] means nums[i] - nums[i-1] => nums[i] = nums[i-1] + diff[i]
Title | Solution | Notes |
---|---|---|
370. Range Addition | ✅ | |
1094. Car Pooling | Solution | ✅ Can also be solved via sweep line |
1109. Coporate Flight Bookings | solution | ✅Can also be solved via sweep line |
DP
Title | Solution | Notes |
---|---|---|
413. Arithmetic Slices |
Tree Problems
BST
- 938. Range Sum of BST
- 98. Validate Binary Search Tree
- left_max < root.val < right_min
- similar problem: 333. Largest BST Subtree
Path
- 437. Path Sum III (prefix_sum)
- 124. Binary Tree Maximum Path Sum✅
- 129. Sum Root to Leaf Numbers✅
- 1022. Sum of Root To Leaf Binary Numbers - similar problem
Ancestor
- 1022. Sum of Root To Leaf Binary Numbers - similar problem
- 235. Lowest Common Ancestor of a Binary Search Tree
- 236. Lowest Common Ancestor of a Binary Tree ✅
- 1026. Maximum Difference Between Node and Ancestor
- 1123. Lowest Common Ancestor of Deepest Leaves
- 1483. Kth Ancestor of a Tree Node ❓
DFS
- 298. Binary Tree Longest Consecutive Sequence
- 549. Binary Tree Longest Consecutive Sequence II
- dfs return [inc, dec], inc means the longest length the array is increase
- 543. Diameter of Binary Tree
- depth problem
Others
Binary Search
- 4. Median of Two Sorted Arrays
- 33. Search in Rotated Sorted Array (left = 0, right = len(nums) - 1 左闭右闭)
- 34. Find First and Last Position of Element in Sorted Array (left = -1, right = len(nums), 左开右开)
- 35. Search Insert Position(left = -1, right = len(nums), [0,left] < target, [right, len(nums)] > target)
- 69. Sqrt(x)(左开右开, left is the largest which makes nums[i] * nums[i] < x)
- 74. Search a 2D Matrix
- i, j = mid // row_len, mid % row_len
- Related probelm - 240. Search a 2D Matrix II it’s more complicated, and can be solved by divide and conquer.
- 153. Find Minimum in Rotated Sorted Array
- 154. Find Minimum in Rotated Sorted Array II
- 540. Single Element in a Sorted Array
- case1: nums[mid] == nums[mid-1]
- case2: nums[mid] == numd[mid+1]
- 875. Koko Eating Bananas
- left, right = 1, max(piles)
- similar problem 1011. Capacity To Ship Packages Within D Days
- 981. Time Based Key-Value Store Design problem
- 1539. Kth Missing Positive Number
- 287. Find the Duplicate Number O(nlogn) - can solved by slow and fast pointer with O(n) time.
DFS
Graph DFS
- 207. Course Schedule
- construct a dict from prerequisites <course, List[pre]>
- go through courses,
- return True if no prerequisites
- return False if visit already
- add course into visit set and dfs its children
- set pre to [] if the course can be completed to avoid duplicate dfsß
- remember to remove the course from visit
- 130. Surrounded Regions
- dfs and mark boarder ‘O’s, other ‘O’s can be changed to ‘X’
Islands
- 200. Number of Islands - high frequency
- similar question 419. Battleships in a Board
- solution
- 305. Number of Islands II
- union find -694. Number of Distinct Islands
- 695. Max Area of Island
- 1905. Count Sub Islands
- 463. Island Perimeter
- don’t need DFS
Topological Sort
- 210. Course Schedule II
- 269. Alien Dictionary
- 444. Sequence Reconstruction
- 310. Minimum Height Trees
- It can be considered as a graph problem
- the roots should be in the longest path, and they are the center of the longest path
Backtracking
- 79. Word Search
- similar problem: 489. Robot Room Cleaner
- 22. Generate Parentheses
- 78. Subsets
- 46. Permutations
- 77. Combinations
- 784. Letter Case Permutation
- 1087. Brace Expansion
Stack
Monotonic Stack (单调栈)
- 84. Largest Rectangle in Histogram //TODO
- 456. 132 Pattern //TODO
- 496. Next Greater Element I
- 739. Daily Temperatures
- 239. Sliding Window Maximum
- maintain a decreasing deque
Greedy
Heap
- 347. Top K Frequent Elements
- python heap: heapq.nlargest(k, count.keys(), key=count.get)
Some Tricks