LeetCode Dynamic Programming (1) Overview of Dynamic Planning Ideas & Basic Topics

dynamic programming

Remember the 5 steps of moving back:
1. Determine the meaning of the dp array (dp table) and the subscript

2. Determine the recursive formula

3. How to initialize the dp array

4. Determine the traversal order

5. Example derivation dp array

Opening - Basic Topics

1. lc509 Fibonacci numbers

describe:

The sequence of Fibonacci numbers (usually denoted by F(n)) is called the Fibonacci sequence. The sequence starts with 0 and 1, and each subsequent number is the sum of the previous two numbers. Given n , compute F(n) . where F(0) = 0 and F(1) = 1

Example:

Input: n = 3
Output: 2
Explanation: F(3) = F(2) + F(1) = 1 + 1 = 2

Solution:
According to the previous writing method, direct recursion is enough, but the complexity of the algorithm must be high. Note that there are actually repeated calculations throughout the recursive process;

For example, F(6)=F(5)+F(4), and then calculate F(5) and F(4) respectively, but when calculating F(5), you will also encounter calculation F(4), but at this time they The two have separated from each other.

Actually that's what we saidA problem has many overlapping subproblems

public int fib(int n) {
    
    
        if(n<=1)
        return n;
        // 1.dp数组的含义:第i个数的斐波那契数值是dp[i]
        int[] dp = new int[n+1];
        
        // 2. 递推公式
        // dp[i]=dp[i-1]+dp[i-2];

        // 3.初始化
        dp[0]=0;
        dp[1]=1;
        
        // 4.遍历顺序
        for(int i=2;i<n+1;i++)
        {
    
    
            dp[i]=dp[i-1]+dp[i-2];
        }
        return dp[n];
    }

2. lc746 Use the minimum cost to climb stairs

describe:

You are given an integer array cost, where cost[i] is the cost to climb up the i-th step of the stairs. Once you pay this fee, you can choose to climb a flight of stairs or two.
You can choose to start climbing the stairs from the step with subscript 0 or subscript 1. Please calculate and return the minimum cost to reach the top of the stairs.

Example:

Input: cost = [1,100,1,1,1,100,1,1,100,1]
Output: 6

Solution:

 public int minCostClimbingStairs(int[] cost) {
    
    
        int len = cost.length;
        // 1.dp数组含义:到达第i台阶所花费的最少体力为dp[i]

        // 2.递推公式
        // dp[i] = Math.min(dp[i-1]+cost[i-1],dp[i-2]+cost[i-2]);
         
        int[] dp = new int[len+1];
        // 3. 初始化
        dp[0] = 0;
        dp[1] = 0;
        // 4. 遍历
        for(int i=2;i<=len;i++)
        {
    
    
            dp[i] = Math.min(dp[i-1]+cost[i-1],dp[i-2]+cost[i-2]);
        }
        return dp[len];
    }

3. lc63 different paths II

describe:

A robot can only move down or to the right one step at a time, trying to reach the lower right corner of the grid, now consider that there are obstacles in the grid. So how many different paths will there be from top left to bottom right? Obstacles and empty positions in the grid are represented by 1 and 0 respectively

Example:

Input: obstacleGrid = [[0,0,0],[0,1,0],[0,0,0]]
Output: 2

Solution:
Compared with the 62 questions, there is one more "obstacle"

 public int uniquePathsWithObstacles(int[][] obstacleGrid) {
    
    
        int m = obstacleGrid.length;
        int n = obstacleGrid[0].length;

        int[][] dp = new int[m][n];

         //如果在起点或终点出现了障碍,直接返回0
        if (obstacleGrid[m - 1][n - 1] == 1 || obstacleGrid[0][0] == 1) {
    
    
            return 0;
        }

        for(int i=0;i<m && obstacleGrid[i][0]==0;i++)
        {
    
    
            dp[i][0]=1;
        }

        for(int i=0;i<n && obstacleGrid[0][i]==0;i++)
        {
    
    
            dp[0][i]=1;
        }
        

        for(int i=1;i<m;i++)
        {
    
    
            for(int j=1;j<n;j++)
            {
    
    
                if(obstacleGrid[i][j]==1)
                dp[i][j]=0;
                else
                dp[i][j] = dp[i][j-1]+dp[i-1][j];
            }
        }
        return dp[m-1][n-1];
    }

4. lc343 integer split

lc343 link

describe:

Given a positive integer n, split it into sums of k positive integers ( k >= 2 ) and maximize the product of these integers. returns the largest product you can get

Example:

Input: Input: n = 10
Output: 36 (10 = 3 + 3 + 4, 3 × 3 × 4 = 36)

Solution:
Pay attention to the recursive formula:
insert image description here

public int integerBreak(int n) {
    
    
        int[] dp = new int[n+1];

        if(n==2)
           dp[n]=1;
   
        for(int i=3;i<=n;i++)
        {
    
    
            int curMax=0;
            for(int j=1;j<i;j++)
            {
    
    
                curMax = Math.max(curMax,Math.max(j*(i-j),j*dp[i-j]));
            }
            dp[i] = curMax;
        }

        return dp[n];
    }

Guess you like

Origin blog.csdn.net/ji_meng/article/details/127743768