Leetcode for A&D
Graph traversal and dynamic programming problems, in a sensible order
The dynamic programming problems will start making way more sense after the course actually gets there. I would still recommend looking into DP on your own time, because it’s just such as big part of the exams (20/80 points last year). You can get ahead early and chill during Lernphase.
Dynamic programming
The best problems from The Ultimate Dynamic Programming Roadmap on r/leetcode.
1. Warm-up
Enough to get a feel for what a DP state is.
- 70. Climbing Stairs, in linear time Easy
- 1137. N-th Tribonacci Number, in linear time Easy
- 279. Perfect Squares, optional Medium
2. One sequence, constant transition
Solve the sub-problem on every prefix of the array, that is every subarray from 0 to i. Each state looks back a fixed number of steps.
- 746. Min Cost Climbing Stairs, in linear time Easy
- 1578. Minimum Time to Make Rope Colorful Medium
- 198. House Robber Medium
- 91. Decode Ways Medium
- 983. Minimum Cost For Tickets Medium
- 2140. Solving Questions With Brainpower Medium
3. Grids
The table has the same shape as the grid, and the state at cell (i, j) relates to the grid at (i, j).
- 62. Unique Paths Medium
- 63. Unique Paths II Medium
- 64. Minimum Path Sum Medium
- 1277. Count Square Submatrices with All Ones Medium
- 221. Maximal Square Medium
- 174. Dungeon Game Hard
4. Two sequences
dp[i][j] is the answer for the first i entries of one sequence against the first j of the other.
- 1143. Longest Common Subsequence Medium
- 1035. Uncrossed Lines, longest common subsequence in disguise Medium
- 712. Minimum ASCII Delete Sum for Two Strings Medium
- 72. Edit Distance Medium
- 115. Distinct Subsequences Hard
- 1092. Shortest Common Supersequence Hard
5. Intervals
The problem is solved on every interval of the array, not just every prefix.
- 516. Longest Palindromic Subsequence Medium
- 1690. Stone Game VII Medium
- 647. Palindromic Substrings Medium
- 1130. Minimum Cost Tree From Leaf Values, the interval is hard to spot Medium
- 664. Strange Printer Hard
- 312. Burst Balloons Hard
6. One sequence, transition from every earlier index
Prefixes again, but the state at i is built from every j < i. Longest increasing subsequence is the model.
- 1395. Count Number of Teams Medium
- 300. Longest Increasing Subsequence Medium
- 1043. Partition Array for Maximum Sum Medium
- 813. Largest Sum of Averages Medium
- 1105. Filling Bookcase Shelves Medium
7. Knapsack
The state is the classical knapsack state: how much of the input used, and the total so far.
- 416. Partition Equal Subset Sum Medium
- 1155. Number of Dice Rolls With Target Sum Medium
- 377. Combination Sum IV Medium
- 474. Ones and Zeroes Medium
- 322. Coin Change Medium
- 518. Coin Change II Medium
- 494. Target Sum Medium
- 1049. Last Stone Weight II Medium
- 879. Profitable Schemes Hard
8. Topological order on graphs
Optional. Solve the problem on every subgraph reachable from a node.
- 1048. Longest String Chain Medium
- 329. Longest Increasing Path in a Matrix Hard
- 630. Course Schedule III Hard
9. Trees
Optional. Solve the problem on every subtree.
- 337. House Robber III Medium
- 968. Binary Tree Cameras Hard
Graph traversal: DFS and BFS
Graph traversal is the second most important thing you learn in this course. You have to know DFS/BFS by heart for the exam. This is also extremely common on Quant/SWE internship interviews so get ahead.
1. First traversals
The traversal on its own, with nothing else going on. Start on trees: a binary tree is the simplest graph there is, and the traversal is just recursion. Handle this node, then call yourself on the children. Do not skip these because they look trivial, this is where the pattern is learned.
- 144. Binary Tree Preorder Traversal, the traversal itself, nothing more Easy
- 94. Binary Tree Inorder Traversal, same code, one line moved Easy
- 226. Invert Binary Tree Easy
- 101. Symmetric Tree, recurse on two nodes at once Easy
- 104. Maximum Depth of Binary Tree Easy
- 733. Flood Fill, the same idea on a grid Easy
- 1971. Find if Path Exists in Graph, now you need a visited set Easy
2. Grids
A grid is a graph whose neighbours are the four adjacent cells. Seeing that is most of the work.
- 200. Number of Islands Medium
- 695. Max Area of Island Medium
- 130. Surrounded Regions, start from the border Medium
- 79. Word Search, DFS with backtracking Medium
- 417. Pacific Atlantic Water Flow, two traversals, then intersect Medium
3. Multi-source BFS
Push every starting cell into the queue before the first step. The layers then give distances to the nearest source.
- 994. Rotting Oranges Medium
- 542. 01 Matrix Medium
- 1926. Nearest Exit from Entrance in Maze Medium
- 934. Shortest Bridge, DFS to find one island, then BFS out from it Medium
4. Graphs
Adjacency lists, connected components, cycles and topological order.
- 133. Clone Graph Medium
- 547. Number of Provinces Medium
- 841. Keys and Rooms Medium
- 785. Is Graph Bipartite?, two-colouring Medium
- 207. Course Schedule, cycle detection Medium
- 210. Course Schedule II, topological sort Medium
5. Shortest paths without weights
Exactly where BFS beats DFS. Knowing why is worth an exam question.
- 1091. Shortest Path in Binary Matrix Medium
- 752. Open the Lock, the states are the graph Medium
- 127. Word Ladder Hard
Binary search
Always good to know.
1. The loop itself
Write it once from memory, then again tomorrow. Everything else here is this loop with a different condition.
- 704. Binary Search, the plain version Easy
- 35. Search Insert Position, what to return when it is missing Easy
- 278. First Bad Version Easy
- 374. Guess Number Higher or Lower Easy
- 69. Sqrt(x), there is no array to search Easy
2. Boundaries
Finding the first or last index that satisfies a condition. This is where the off-by-one errors live, so do all of them.
- 744. Find Smallest Letter Greater Than Target Easy
- 34. Find First and Last Position of Element in Sorted Array, two searches Medium
- 852. Peak Index in a Mountain Array Medium
- 162. Find Peak Element, the array is not sorted Medium
3. When the array is not plainly sorted
Sorted, but rotated or folded into two dimensions. The invariant is still there, you just have to find it.
- 33. Search in Rotated Sorted Array Medium
- 153. Find Minimum in Rotated Sorted Array Medium
- 74. Search a 2D Matrix Medium
- 240. Search a 2D Matrix II, not a binary search, but the same instinct Medium
4. Binary search on the answer
The one worth knowing. There is no array: you guess an answer, check whether it is achievable, and halve the range. Once you see it you will see it everywhere.
- 875. Koko Eating Bananas, start here Medium
- 1011. Capacity To Ship Packages Within D Days Medium
- 1283. Find the Smallest Divisor Given a Threshold Medium
- 1482. Minimum Number of Days to Make m Bouquets Medium
- 410. Split Array Largest Sum Hard
- 4. Median of Two Sorted Arrays, optional Hard