Fibonacci Series
These problems are the entry point to dynamic programming: linear DP on a single 1-D index, where the state at i depends only on a handful of earlier states. Each one is presented through the same progression - start with the bare recurrence memoized by an LRU cache, make the memo table explicit, turn it bottom-up into a tabulation, then shrink that table to the last few cells for O(k) space. Seeing one recurrence in all four forms is the fastest way to internalize the top-down to bottom-up transformation.
Fibonacci Series Extension
509. Fibonacci Number
The recurrence is the classic fib(i) = fib(i - 1) + fib(i - 2) with base cases fib(0) = 0 and fib(1) = 1. Here we write the bare top-down recursion and let @lru_cache memoize every call automatically, so each subproblem is computed once. This keeps the code as close to the mathematical definition as possible.
- Time
- O(n)
- With memoization each of the
nsubproblems is solved exactly once. - Space
- O(n)
- The cache plus the recursion stack hold up to
nentries.
70. Climbing Stairs
The number of ways to reach step i is ways(i) = ways(i - 1) + ways(i - 2), the same shape as Fibonacci, with base cases handled by i <= 2. We write the bare recursion and let @lru_cache memoize each subproblem automatically so it is only solved once.
- Time
- O(n)
- Each of the
nsubproblems is computed once thanks to the cache. - Space
- O(n)
- The cache plus recursion stack hold up to
nentries.
1137. N-th Tribonacci Number
The Tribonacci recurrence sums the previous three values: trib(i) = trib(i - 1) + trib(i - 2) + trib(i - 3), with trib(0) = 0 and trib(1) = trib(2) = 1. We write the bare recursion and let @lru_cache memoize each subproblem automatically.
- Time
- O(n)
- Each of the
nsubproblems is solved once with caching. - Space
- O(n)
- The cache plus recursion stack hold up to
nentries.
Staircase
Given a stair with n steps, implement a method to count how many possible ways are there to reach the top of the staircase, given that, at every step you can either take 1 step, 2 steps, or 3 steps.
Example 1:
Number of stairs (n) : 3
Number of ways = 4
Explanation: Following are the four ways we can climb : {1, 1, 1}, {1, 2}, {2, 1}, {3}
Example 2:
Number of stairs (n) : 4
Number of ways = 7
Explanation: Following are the seven ways we can climb : {1, 1, 1, 1}, {1, 1, 2}, {1, 2, 1}, {2, 1, 1}, {2, 2}, {1, 3}, {3, 1}
Reaching step i from one, two, or three steps below gives ways(i) = ways(i - 1) + ways(i - 2) + ways(i - 3), with base cases i <= 1 and i == 2. We write the bare recursion and let @lru_cache memoize each subproblem automatically.
- Time
- O(n)
- Each of the
nsubproblems is solved once with caching. - Space
- O(n)
- The cache plus recursion stack hold up to
nentries.
Number Factors
Given a number n, implement a method to count how many possible ways there are to express n as the sum of 1, 3, or 4.
Example 1:
n : 4
Number of ways = 4
Explanation: Following are the four ways we can express 'n' : {1, 1, 1, 1}, {1, 3}, {3, 1}, {4}
Example 2:
n : 5
Number of ways = 6
Explanation: Following are the six ways we can express 'n' : {1, 1, 1, 1, 1}, {1, 1, 3}, {1, 3, 1}, {3, 1, 1}, {1, 4}, {4, 1}
Each expression ends in a 1, 3, or 4, so ways(i) = ways(i - 1) + ways(i - 3) + ways(i - 4), with base cases handled by i <= 2 and i == 3. We write the bare recursion and let @lru_cache memoize each subproblem automatically.
- Time
- O(n)
- Each of the
nsubproblems is solved once with caching. - Space
- O(n)
- The cache plus recursion stack hold up to
nentries.
Min / Max, Top-Down (N to 0)
746. Min Cost Climbing Stairs
To reach step i we either come from i - 1 paying cost[i - 1] or from i - 2 paying cost[i - 2], so f(i) = min(cost[i - 1] + f(i - 1), cost[i - 2] + f(i - 2)), with f(i) = 0 for i <= 1. We write the bare recursion and let @lru_cache memoize each subproblem automatically.
- Time
- O(n)
- Each of the
nsubproblems is solved once with caching. - Space
- O(n)
- The cache plus recursion stack hold up to
nentries.
Minimum Jumps with Fee
Given a staircase with n steps and an array of n numbers representing the fee that you have to pay if you take the step. Implement a method to calculate the minimum fee required to reach the top of the staircase (beyond the top-most step). At every step, you have an option to take either 1 step, 2 steps, or 3 steps. You should assume that you are standing at the first step.
Example 1:
Number of stairs (n) : 6
Fee: {1, 2, 5, 2, 1, 2}
Output: 3
Explanation: Starting from index '0', we can reach the top through: 0->3->top The total fee we have to pay will be (1+2).
Example 2:
Number of stairs (n): 4
Fee: {2, 3, 4, 5}
Output: 5
Explanation: Starting from index '0', we can reach the top through: 0->1->top The total fee we have to pay will be (2+3).
Reaching step i costs the fee of the step we jumped from plus the best cost to that step: f(i) = min(fee[i - 1] + f(i - 1), fee[i - 2] + f(i - 2), fee[i - 3] + f(i - 3)), with the base case folded into i <= 3. We write the bare recursion and let @lru_cache memoize each subproblem automatically.
- Time
- O(n)
- Each of the
nsubproblems is solved once with caching. - Space
- O(n)
- The cache plus recursion stack hold up to
nentries.
198. House Robber
198House Robber
At house i we either rob it (taking nums[i] plus the best from i - 2) or skip it (taking the best from i - 1), giving f(i) = max(nums[i] + f(i - 2), f(i - 1)), with f(i) = 0 for i < 0. We write the bare recursion and let @lru_cache memoize each subproblem automatically.
- Time
- O(n)
- Each of the
nsubproblems is solved once with caching. - Space
- O(n)
- The cache plus recursion stack hold up to
nentries.
213. House Robber II
The houses are in a circle, so the first and last cannot both be robbed. We split into two linear sub-problems - rob houses nums[1:] (skip the first) or rob houses nums[:-1] (skip the last) - and take the larger. Here rob_simple() is the same function as rob() in the simple version of this problem (198. House Robber).
- Time
- O(n)
- Two linear passes over the houses, each
O(n). - Space
- O(n)
- Slicing creates two arrays of size up to
n, plus thedpused insiderob_simple().
Min / Max, Bottom-Up (0 to N)
Minimum Jumps to Reach the End
Given an array of positive numbers, where each element represents the max number of jumps that can be made forward from that element, write a program to find the minimum number of jumps needed to reach the end of the array (starting from the first element). If an element is 0, then we cannot move through that element.
Example 1:
Input = 4
Output = 3
Explanation: Starting from index '0', we can reach the last index through: 0->2->3->4
Example 2:
Input = 3
Output = 4
Explanation: Starting from index '0', we can reach the last index through: 0->1->2->3->8
Unlike the earlier problems this builds forward from index 0: from i we try every reachable i + j and take f(i) = 1 + min over j of f(i + j), with f(n - 1) = 0 at the end. We write the bare recursion and let @lru_cache memoize each subproblem automatically.
- Time
- O(n^2)
- Each of the
nstates scans up to its jump range, so in the worst case the work is quadratic. - Space
- O(n)
- The cache plus recursion stack hold up to
nentries.