Dynamic Programming Summary I


Summary of Dynamic Programming I. (Basics)

DP 5 Steps:

  1. Define dp table, i, j
  2. Determine recurrent relation
  3. Initialize dp table
  4. Determine iteration order to fill in the table
  5. Take an example to derive recurrent relation

LeetCode 509 Fibonacci Number

class Solution(object):
    def fib(self, n):
        if n == 0: return 0
        dp = [0] * (n+1)
        dp[1] = 1
        for i in range(2, n+1):
            dp[i] = dp[i-1] + dp[i-2]
        return dp[n]

LeetCode 70 Climbing Stairs

Define dp[i] as the number of distinct ways to climb to the ith staircase. Since we can either climb 1 or 2 steps, there are two ways to climb to the ith staircase, either from dp[i-1] by one step or from dp[i-2] by two steps. Therefore we have dp[i] = dp[i-1] + dp[i-2]. From this formula we know that the iteration is going forward from the beginning. The base cases are dp[0] = dp[1] = 1 meaning that there is only 1 way to go to the first stair or stay there.

class Solution:
    def climbStairs(self, n: int) -> int:
        dp=[0]*(n+1)
        dp[0]=1
        dp[1]=1
        for i in range(2,n+1):
            dp[i]=dp[i-1]+dp[i-2]
        return dp[n]

LeetCode 746 Min Cost Climbing Stairs

class Solution:
    def minCostClimbingStairs(self, cost):
        n = len(cost)
        dp = [0] * (n+1)
        for i in range(2, n+1):
            dp[i] = min(dp[i-1] + cost[i-1], dp[i-2] + cost[i-2])
        return dp[n]

LeetCode 62 Unique Paths

Define f(i,j) as the number of paths from (0,0) to (i,j). There are two ways to reach (i,j): from (i-1,j) or from (i,j-1). The recurrence relation is f(i,j) = f(i-1,j) + f(i,j-1). From the recurrent relation we know that the table is filled out from the left top to the right bottom. The base case f(i,0) = 1 because there is only one way from i to 0, same as f(0,j) = 1. The final answer is f(m-1,n-1).

class Solution(object):
    def uniquePaths(self, m, n):
        # f = [[1 for i in range(n)] for j in range(m)]
        dp = [[1] * n for _ in range(m)]
        for i in range(1,m):
            for j in range(1,n):
                dp[i][j] = dp[i-1][j] + dp[i][j-1]
        return dp[m-1][n-1]
class Solution(object):
    def uniquePaths(self, m, n):
        dp = [1] * n
        for i in range(1, m):
            for j in range(1, n):
                dp[j] = dp[j] + dp[j - 1]
        return dp[-1]

LeetCode 63 Unique Paths II

class Solution:
    def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:
        m = len(obstacleGrid)
        n = len(obstacleGrid[0])
        dp = [[0] * n for _ in range(m)]
        
        if obstacleGrid[0][0] == 1: return 0
        dp[0][0] = 1
        
        for i in range(1,n):
            if obstacleGrid[0][i] == 0:
                dp[0][i] = dp[0][i-1]

        for j in range(1,m):
            if obstacleGrid[j][0] == 0:
                dp[j][0] = dp[j-1][0]
                
        for i in range(1,m):
            for j in range(1,n):
                if obstacleGrid[i][j] == 0:
                    dp[i][j] = dp[i-1][j] + dp[i][j-1]

        return dp[m-1][n-1]

LeetCode 343 Integer Break

Define dp[i] as the maximum product of splitting number i. From 1, 2, ..., j, we can get dp[i] in two ways: 1) j * (i-j) if (i-j) cannot be split, 2) j * dp[i-j] if (i-j) can be split, where dp[i-j] is the maximum product of splitting number i-j. And we store the maximum value of the two.

The recurrence relation is dp[i] = max(dp[i], max((i-j) * j, dp[i-j] * j)). The base case: dp[2] = 1. Note that since the base case starts from 2, i starts from 3, j ends in i-1.

class Solution:
    def integerBreak(self, n: int) -> int:
        dp = [0] * (n+1)
        dp[2] = 1

        for i in range(3, n+1):
            for j in range(1, i-1):
                dp[i] = max(dp[i], max(j * dp[i-j], j * (i-j)))

        return dp[n]

LeetCode 96 Unique Binary Search Trees

When n= 1, there is 1 unique BST. When n=2, there are 2 unique BSTs. When n =3, there are 5 unique BSTs. Then we can notice that the number of unique BSTs can actually be calculated by the results of previous subproblems. (5 = 2 + 2 + 1).

When n = 3, dp[3] is # BST with root 1 + # BST with root 2 + # BST with root 3. dp[3] = dp[2] * dp[0] + dp[1] * dp[1] + dp[0] * dp[2].

  • If root is 1, the number of unique BST is: the number of unique BSTs of 2 elements(left) * the number of unique BSTs of 0 element (right).
  • If root is 2, the number of unique BST is: the number of unique BSTs of 1 elements (left) * the number of unique BSTs of 1 element (right).
  • If root is 3, the number of unique BST is: the number of unique BSTs of 0 element (left) * the number of unique BSTs of 2 elements (right).

Define dp[i] as the number of unique BSTs composed of nodes of 1 to i.

The recurrent relation is dp[i] += dp[j-1] * dp[i-j], where dp[j-1] is the number of unique BSTs of the left subtree of the root j, dp[i-j] is the number of unique BSTs of the right subtree if the root j. (e.g. dp[3] += dp[0] * dp[2] when j = 1).

The base case dp[0]=1 because the above recurrent relation is doing multiplication. And dp[1]=1. The final answer is stored in dp[-1].

class Solution:
    def numTrees(self, n: int) -> int:
        dp = [0] * (n+1)
        dp[0] = 1
        dp[1] = 1
        for i in range(2,n+1):
            for j in range(1,i+1):
                dp[i] += dp[j-1] * dp[i-j]
        return dp[-1]

  TOC