Dynamic Programming Flashcards

1
Q

What are the two main techniques used in Dynamic Programming?

A

Memoization (Top-Down), Tabulation (Bottom-Up).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the difference between top-down (memoization) and bottom-up (tabulation) DP?

A

Memoization uses recursion + caching, Tabulation uses loops.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the time complexity of solving Fibonacci with DP?

A

O(n).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How does the Knapsack problem use DP?

A

Optimize item selection within weight constraints.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the difference between DP and Greedy algorithms?

A

DP finds global optimal, Greedy finds local optimal.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are key signals that a problem can be solved using Dynamic Programming?

A

Overlapping Subproblems → Solving the same subproblems repeatedly

Optimal Substructure → The solution is built from smaller solutions

Decision Making → Choosing between different ways to solve subproblems

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what is top down good for

A

Solves only necessary subproblems, faster for sparse problems

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

what is bottom up good for

A

✅ Iterative, no recursion overhead, good for full-table problems

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the three places you need to identify in a dp problem?

A

Key Insight
Base Case
Recurrence Relation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the steps to breaking down a dp problem

A
  1. Identify Overlapping Subproblems

If a problem breaks into smaller versions of itself, DP is a good fit.

  1. Find a Recurrence Relation

Whether bottom-up or top-down, DP is all about finding how the solution for dp[i][j] depends on dp[i+1][j-1] or dp[i+1][j].

  1. Decide Between Recursion (Top-Down) or Iteration (Bottom-Up)

Top-Down: Naturally follows from recursion, helpful in deriving the formula.
Bottom-Up: Helps see dependencies clearly in the DP table.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Go through an example of identifying dp formulas using Longest Palindromic Subsequence problem

A
  1. key insight is if s[i] === s[j] then theres a substring because you can remove letters in between
    2.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What question should you ask to recognize dp problems?

A

Can the solution be expressed in terms of solutions to similar smaller problems?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Whats a good question to ask when trying to understand input params dp?

A

Which inputs/parameters decrease as the problem size reduces

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

what is recurrence formula for fibonacci

A

🟢 1. Fibonacci-Type (Simple Recurrence)
Pattern: Solve dp[i] using dp[i-1] and dp[i-2].
✅ Example: Fibonacci numbers, Climbing Stairs, Tribonacci.
🔹 Formula:

dp[i] = dp[i-1] + dp[i-2]
Base: dp[0]=0, dp[1]=1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

what is recurrence formula for knapsack

A

🔵 2. Knapsack-Type (Subset Selection)
Pattern: Decide whether to include an element or not (binary choice).
✅ Example: 0/1 Knapsack, Subset Sum, Partition Equal Subset Sum.
🔹 Formula:

dp[i][w] = max(dp[i-1][w], val[i] + dp[i-1][w-weight[i]])
Base: dp[0][] = 0 (no items), dp[][0] = 0 (zero capacity)

16
Q

What about for unbouned knapsack

A

🟠 3. Unbounded Knapsack-Type (Infinite Choices)
Pattern: Similar to knapsack but items can be reused.
✅ Example: Coin Change (min coins), Rod Cutting, Integer Break.
🔹 Formula:

dp[i] = min(dp[i - coin] + 1 for coin in coins)
Base: dp[0] = 0

17
Q

What is recurrence formala for longest subsequence?

A

🟣 4. Longest Subsequence-Type
Pattern: Compare characters/elements, build solutions iteratively.
✅ Example: Longest Common Subsequence (LCS), Longest Palindromic Subsequence.
🔹 Formula:

If s[i] === s[j]: dp[i][j] = 2 + dp[i+1][j-1]
Else: dp[i][j] = max(dp[i+1][j], dp[i][j-1])

18
Q

What is recurrence formula for partition problems

A

🟤 6. Partition DP (Splitting Problems)
Pattern: Partition the problem into segments and solve subproblems.
✅ Example: Matrix Chain Multiplication, Burst Balloons, Palindrome Partitioning.
🔹 Formula:

dp[i][j] = min(dp[i][k] + dp[k+1][j] + cost(i,k,j))

19
Q

What is recurrence formula for game theory?

A

🟢 8. Game Theory DP
Pattern: Player strategies with optimal choices.
✅ Example: Predict the Winner, Stone Game, Nim Game.
🔹 Formula:

dp[i][j] = max(arr[i] - dp[i+1][j], arr[j] - dp[i][j-1])