Tabulation Bottom Up DP in DSA Typescript - Time & Space Complexity
We want to understand how the time needed grows when using tabulation in bottom-up dynamic programming.
Specifically, how the number of steps changes as the input size increases.
Analyze the time complexity of the following code snippet.
function fib(n: number): number {
const dp: number[] = new Array(n + 1).fill(0);
dp[0] = 0;
dp[1] = 1;
for (let i = 2; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
This code calculates the nth Fibonacci number using bottom-up tabulation.
- Primary operation: The for-loop that fills the dp array from 2 to n.
- How many times: It runs exactly n-1 times, once for each number from 2 up to n.
As n grows, the loop runs more times, increasing the steps linearly.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 9 additions |
| 100 | About 99 additions |
| 1000 | About 999 additions |
Pattern observation: The number of steps grows roughly in a straight line with n.
Time Complexity: O(n)
This means the time needed grows directly in proportion to the input size.
[X] Wrong: "Dynamic programming always takes exponential time because it solves many subproblems."
[OK] Correct: Tabulation solves each subproblem once in order, so it only takes linear time here, not exponential.
Understanding how tabulation reduces repeated work helps you explain efficient solutions clearly and confidently.
"What if we used recursion with memoization instead of tabulation? How would the time complexity change?"
Practice
Tabulation Bottom Up DP?Solution
Step 1: Understand Tabulation approach
Tabulation means filling a table starting from the smallest subproblems, moving upwards.Step 2: Compare with other methods
Unlike recursion with memoization, tabulation solves subproblems iteratively in order.Final Answer:
Build a table from the smallest subproblems up to the main problem -> Option AQuick Check:
Tabulation = Bottom Up DP [OK]
- Confusing tabulation with recursion
- Thinking tabulation guesses answers
- Mixing memoization with tabulation
n+1 with zeros for tabulation?Solution
Step 1: Check correct array initialization
Usingnew Array(n+1).fill(0)creates an array of length n+1 filled with zeros.Step 2: Identify incorrect options
const dp = Array().map(() => 0); usesmapon empty array which won't fill correctly; const dp = new Array(n).fill(null); fills with nulls; const dp = Array.fill(n+1, 0); is invalid syntax.Final Answer:
const dp = new Array(n+1).fill(0); -> Option AQuick Check:
Array fill with zeros = new Array(n+1).fill(0) [OK]
- Using map on empty array without elements
- Wrong array size (n instead of n+1)
- Using invalid Array.fill syntax
function fib(n: number): number {
const dp = new Array(n + 1).fill(0);
dp[1] = 1;
for (let i = 2; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}
console.log(fib(5));Solution
Step 1: Trace dp array filling for n=5
dp[0]=0, dp[1]=1; then dp[2]=1, dp[3]=2, dp[4]=3, dp[5]=5.Step 2: Return dp[5]
The function returns dp[5] which is 5.Final Answer:
5 -> Option BQuick Check:
Fibonacci(5) = 5 [OK]
- Starting dp[1] as 0 instead of 1
- Off-by-one errors in loop
- Returning dp[n-1] instead of dp[n]
function factorial(n: number): number {
const dp = new Array(n + 1).fill(0);
for (let i = 1; i <= n; i++) {
dp[i] = dp[i - 1] * i;
}
return dp[n];
}
console.log(factorial(4));Solution
Step 1: Check dp initialization and base case
dp is filled with 0s, but dp[0] must be explicitly set to 1 as factorial(0)=1.Step 2: Verify loop correctness
Loop from i=1 to n is correct; dp[i] = dp[i-1] * i is correct formula.Final Answer:
dp[0] should be set to 1 before the loop -> Option DQuick Check:
Base case dp[0]=1 needed [OK]
- Starting loop at 0 causing dp[-1] access
- Wrong initialization of dp array
- Incorrect multiplication formula
n using coins of denominations [1, 3, 4] with bottom-up DP. Which of these is the correct tabulation approach?Solution
Step 1: Understand coin change tabulation
We start dp with Infinity except dp[0]=0 because 0 coins needed for amount 0.Step 2: Update dp for each amount and coin
For each amount i, check all coins; if coin <= i, update dp[i] = min(dp[i], dp[i-coin] + 1).Final Answer:
Initialize dp array with Infinity except dp[0]=0; for each amount i from 1 to n, update dp[i] = min(dp[i], dp[i - coin] + 1) for each coin if i >= coin -> Option CQuick Check:
Coin change DP = min coins with dp and Infinity init [OK]
- Not initializing dp with Infinity
- Ignoring multiple coin options per amount
- Using recursion instead of tabulation
Ready to prove it?
Pass the test and this pattern moves unknown
