~/problems
Problems
Basics first, then the classics, then company-style assessments. Every problem has tests you run right here; multi-level ones unlock as you go. See the roadmap.
Intro DP
State, transition, base case; top-down memo vs bottom-up table.
Notes
Recognise it when: you're asked for a count of ways, a min or max over choices, or "can you reach", and the subproblems overlap.
Four questions: What is the state? What is the transition? What are the base cases? What order do you fill the table in?
# House robber: best up to i = max(skip i, take i)
prev2 = prev1 = 0
for x in nums:
prev2, prev1 = prev1, max(prev1, prev2 + x)
- Top-down:
@functools.cacheon a recursive function is fastest to write, but watch the recursion limit. - Bottom-up plus rolling variables gives O(1) memory.
- Decode ways:
dp[i] = dp[i-1] (one valid digit) + dp[i-2] (valid 10..26). The trap is "0".
17 problems
Interview roadmap
1-D dynamic programming One index of state: stairs, robbers, subsequences.
Intro DP guide
- Basics: Cheapest way up a staircase basics py · c++ · java easy
- Flower cart: station or park py · c++ · java easy
- Climbing Stairs py · c++ · java easy
- House Robber py · c++ · java easy
- Decode Ways medium
- Maximum Subarray py · c++ · java easy
- Ordered dice rolls that hit a target sum py · c++ · java easy
- Count schedules with no process in two slots in a row Citadel py · c++ · java easy
- Collatz Sequence Steps AirbnbBloomberg medium
- Knight hops on a phone keypad Citadel py · c++ · java medium
- Best score with prime-3 jumps Uber py · c++ · java medium
- Word Break py · c++ · java medium
- Cheapest climb with strides of one to three steps py · c++ · java easy
- House Robber II py · c++ · java medium
- Maximum Product Subarray py · c++ · java medium
- Best Time to Buy and Sell Stock with Cooldown py · c++ · java medium
- Maximum Sum Circular Subarray py · c++ · java medium