|
| 1 | +--- |
| 2 | +title: 'dynamic programming 101' |
| 3 | +tags: 'journal' |
| 4 | +date: 'Mar 27, 2025' |
| 5 | +--- |
| 6 | + |
| 7 | +took the time to learn the framework for dp. below are some notes from the leetcode explore for DP. |
| 8 | + |
| 9 | +characteristics of dp: |
| 10 | + |
| 11 | +- break down into **overlapping** subproblems |
| 12 | +- an optimal solution can be formed |
| 13 | + |
| 14 | +what dp is not: |
| 15 | + |
| 16 | +- **greedy** problems have optimal substructure, but not overlapping subproblems |
| 17 | +- **divide and conquer** breaks a problem into subprobelms, but they are not overlapping |
| 18 | + |
| 19 | +why it helps? |
| 20 | + |
| 21 | +- it improves time complexity compared to brute force |
| 22 | +- ex: fib(n) has exponential time complexity for brute force, while linear with DP, as it reuses results of subproblems raather than recalculating results for previously seen subproblems |
| 23 | + |
| 24 | +top-down vs bottom up |
| 25 | + |
| 26 | +- top down (tabulation) |
| 27 | + |
| 28 | + - **iteration** and starts with a basecase |
| 29 | + - order matters |
| 30 | + - usually faster, no recursion overhead |
| 31 | + |
| 32 | + ```py |
| 33 | + # fibonacci |
| 34 | + F = [0] * (n+1) |
| 35 | + F[0] = 1 |
| 36 | + F[1] = 1 |
| 37 | + for i in range(2, n): |
| 38 | + F[i] = F[i-1] + F[i-2] |
| 39 | + ``` |
| 40 | + |
| 41 | +- bottom-up (memoization) |
| 42 | + |
| 43 | + - **recursion** and made efficient with memoization |
| 44 | + - easier to write, order does not matter |
| 45 | + - slower |
| 46 | + |
| 47 | + ```py |
| 48 | + memo = {} |
| 49 | + def f(i): |
| 50 | + if i < 2: |
| 51 | + return i |
| 52 | + |
| 53 | + if i not in memo: |
| 54 | + memo[i] = f(i-1) + f(i-2) |
| 55 | + |
| 56 | + return memo[i] |
| 57 | + ``` |
| 58 | + |
| 59 | +when to use it? |
| 60 | + |
| 61 | +1. problem asks for optimum value (max/min) of something |
| 62 | + - min cost of doing ... , how many ways are there to ... , longest possible ... |
| 63 | + - not enough by itself, could be greedy |
| 64 | +2. future "decisions" depends on earlier decisions |
| 65 | + - ex: house robber |
| 66 | + - nums = [2,7,9,3,1] |
| 67 | + - greedy solution is to rob 7, but you miss out on 9 (your early decision affects future decisions) |
| 68 | + - ex: longest increasing subsequence |
| 69 | + - nums = [1,2,6,3,5] |
| 70 | + - important decision is choosing 6 or not, this affects the future (whether you can take 3 and 5) |
| 71 | + |
| 72 | +framework |
| 73 | + |
| 74 | +1. a function that computes answer to problem for every given state |
| 75 | + - ex: climbing stairs, we have dp(i) which returns number of ways to climb ith step. |
| 76 | +2. a recurrence relation to transition between states |
| 77 | + - to climb the 10th stair, we need to climb from the 8th or 9th |
| 78 | + - so # ways to climb 10th stair is # ways to climb 8th + # ways to climb 9th stair |
| 79 | + - dp(i) = dp(i-1) + dp(i-2) |
| 80 | + - finding this is the most difficult part |
| 81 | +3. base cases (to prevent it going infinitely) |
| 82 | + - ask yourself: what state can you find answer without using DP? |
| 83 | + - there is one way to climb first stair, and two ways to climb two stairs, |
| 84 | + - base case = dp(1) = 1 and dp(2) = 2 |
| 85 | + |
| 86 | +recurrence -> O(2^n) |
| 87 | + |
| 88 | +```py |
| 89 | +def climbStairs(self, n: int) -> int: |
| 90 | + def dp(i): |
| 91 | + """A function that returns the answer to the problem for a given state.""" |
| 92 | + # Base cases: when i is 3 there are i ways to reach the ith stair. |
| 93 | + if i <= 2: |
| 94 | + return i |
| 95 | + |
| 96 | + # If i is not a base case, then use the recurrence relation. |
| 97 | + return dp(i - 1) + dp(i - 2) |
| 98 | + |
| 99 | + return dp(n) |
| 100 | +``` |
| 101 | + |
| 102 | +add memoization -> O(n) |
| 103 | + |
| 104 | +```py |
| 105 | +def climbStairs(self, n: int) -> int: |
| 106 | + def dp(i): |
| 107 | + if i <= 2: |
| 108 | + return i |
| 109 | + if i not in memo: |
| 110 | + # Instead of just returning dp(i - 1) + dp(i - 2), calculate it once and then |
| 111 | + # store the result inside a hashmap to refer to in the future. |
| 112 | + memo[i] = dp(i - 1) + dp(i - 2) |
| 113 | + |
| 114 | + return memo[i] |
| 115 | + |
| 116 | + memo = {} |
| 117 | + return dp(n) |
| 118 | +``` |
| 119 | + |
| 120 | +bottom up approach |
| 121 | + |
| 122 | +```py |
| 123 | +def climbStairs(self, n: int) -> int: |
| 124 | + if n == 1: |
| 125 | + return 1 |
| 126 | + |
| 127 | + # An array that represents the answer to the problem for a given state |
| 128 | + dp = [0] * (n + 1) |
| 129 | + |
| 130 | + # base case |
| 131 | + dp[1] = 1 |
| 132 | + dp[2] = 2 |
| 133 | + |
| 134 | + for i in range(3, n + 1): |
| 135 | + dp[i] = dp[i - 1] + dp[i - 2] # Recurrence relation |
| 136 | + |
| 137 | + return dp[n] |
| 138 | +``` |
0 commit comments