Dynamic programming Flashcards

(12 cards)

1
Q

LeetCode 70 – Climbing Stairs
Distinct ways to climb n stairs (1 or 2 steps).

A

dp[i] = dp[i-1] + dp[i-2].

Base: dp[0]=1, dp[1]=1.

Space optimize with two vars.

O(n) time, O(1) space.

Tag: Fibonacci DP.

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

LeetCode 746 – Min Cost Climbing Stairs
Min cost to reach top (pay cost[i] when step on i).

A

dp[i] = cost[i] + min(dp[i-1], dp[i-2]).

Answer = min(dp[n-1], dp[n-2]).

Space O(1).

Tag: DP on steps.

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

LeetCode 198 – House Robber
Max money w/o robbing adjacent houses.

A

dp[i] = max(dp[i-1], dp[i-2] + nums[i]).

Initialize dp[0]=nums[0], dp[1]=max(nums[0],nums[1]).

Space optimize with two vars.

Tag: DP, Choose/Skip.

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

LeetCode 213 – House Robber II
Like #198 but houses in a circle.

A

Break into two cases: [0..n-2] and [1..n-1].

Run House Robber on both, take max.

Tag: DP on circle, subproblem split.

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

LeetCode 5 – Longest Palindromic Substring
Longest palindrome in s.

A

Expand Around Center O(n²) or DP table:

dp[l][r]=true if s[l]==s[r] && (r-l<3 || dp[l+1][r-1]).

Track longest.

Tag: 2-pointer center or DP.

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

LeetCode 647 – Palindromic Substrings
Count all palindromic substrings.

A

Expand around every center (2n-1) counting valid palindromes.

Or DP like #5 but count instead of max.

O(n²) time.

Tag: DP / Expand center.

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

LeetCode 91 – Decode Ways
Count ways to decode digits to letters.

A

dp[i] = (s[i-1]!=’0’? dp[i-1] : 0) + (valid two-digit? dp[i-2] : 0).

Base: dp[0]=1.

O(n) time, O(1) space.

Tag: DP on string.

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

LeetCode 322 – Coin Change
Fewest coins to make amount.

A

dp[0]=0; for a in [1..amount], dp[a]=1+min(dp[a-c]).

Init dp with ∞.

O(n*amount).

Tag: Unbounded knap, Bottom-up.

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

LeetCode 152 – Maximum Product Subarray
Largest product of contiguous subarray.

A

Track curMax, curMin (negatives swap).

At each num: update max(num, numprevMax, numprevMin) and min similarly.

Keep global best.

O(n) time, O(1) space.

Tag: DP, min/max trick.

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

LeetCode 139 – Word Break
Can s be segmented into dict words?

A

dp[i] true if ∃ word w in dict where s[i-len(w):i]==w && dp[i-len(w)].

Init dp[0]=true.

O(n²) time.

Tag: DP on prefix, HashSet.

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

LeetCode 300 – LIS
Length of longest increasing subsequence.

A

DP O(n²): dp[i]=1+max(dp[j]|j<i & nums[j]<nums[i]).

Optimize O(n log n): patience sorting w/ tails array + binary search.

Tag: DP or Greedy+BS.

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

LeetCode 416 – Partition Equal Subset Sum
Can nums be split into 2 equal-sum subsets?

A

Target = sum/2 (if odd → false).

0/1 knapsack: dp[x]=true if x reachable.

Iterate nums: for val in [target..val] desc: dp[val] |= dp[val-num].

O(n*sum/2).

Tag: DP on subset sums.

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