(W6) Dynamic Programming Flashcards

(9 cards)

1
Q

What is the big idea of Dynamic Programming?

A

Memo-ize the smaller problems, so we don’t solve the same problem more than once

(think fib numbers, if we don’t memoize we are gonna repeat calculations for ages)

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

Bottom up approach vs top down approach

Pros and cons of each

A

Bottom up avoids recursion (lower space complexity), but often has to calculate each subproblem from {0, n}

Top down often can just calculate the required problems - but has to use recursion (so increased stack space)

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

Overlapping subproblems vs optimal substructure?

A

Overlapping subproblems - problems that will be solved multiple times

Optimal substructure - when a solution can be found from the values of it’s overlapping subproblems

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

How to find the longest increasing subsequence of an array (using DP)

A

Overlapping Subproblems: DP[i] = the length of the longest increasing subsequence that ends at i

Logically: an increasing subsequences at x cannot include a value higher than x in the previous part of the array. so you could select any value - but we want the longest increasing subsequence, so pick the maximum value in the preceding array

Value of DP[i] is the maximum number between 0 < and i. DP[i] = DP[of this max number] + 1

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

How to find the subarray (continuous) of array a with the maximum sum

A

DP[i] = { sum of all elements in a[1..i] }

max sum will be the max value of DP[j] - DP[i] - where 0 <= i <= j <= n, so try each combination

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

Recurrence of the Coin Change problem?

A

Trying to make $v

  1. 0 if v = 0
  2. inf if v > 0 and v < c[i] for all i
    - this means that all coins are larger than $v (so not possible)
  3. min(1 + mincoins[v - c[i])
    (where i is the index of the coins provided)
    (cost of each coin must be <= v)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Solve the ‘cross the road and pickup the rock or stay on your side’ problem with dynamic programming

A

Keep two DP arrays
DPleft[i] = {max amount if we start from pavement at i}
DPright[i] = {max amount if we start from pavement at i}

Approach, work your way down

DPleft:
- left[n] if i = n (so base case)
otherwise, max of left[i] + DPleft[i+1] OR switch sides DPright[i+1]

Same for DPright

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

Solve the ‘you can only take the first or last coin problem, what is the total value of coins you can take’ problem with Dynamic Programming

A

DP[i , j ] = {The maximum score that we can obtain playing first from the subarray a [i.. j ]}

Note that if I take the first coin, then my friend will take the max value from [1, n] coins

So I will be able to get whats left over - sum(1,n) - dp(1,n)

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

What is the recurrence relation with 0/1 Knapsack problem?

A

Max[i,c] = {max value we can fit in a capacity of c using items 1 to i}

Two options:
Either weight of item i is > capacity, in which case, use i-1 items
Or pick the max between taking i-1 items, or taking current item + i-1 items and remaining space

max[i,c] = 0 if i = 0
max[i-1,c] if wi > c
max(max[i-1], c) OR value + max[i-1, c-weight)

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