[Likou Brush Questions | Day 22]

Table of contents

Foreword:

63. Different Paths II - LeetCode

 343. Integer Split - LeetCode

 Summarize:


Foreword:

Today we broke through the topic of the dynamic programming chapter. I have also written an article on the introduction of related algorithm theory: [Data Structure and Algorithms in the Dead of Night | Chapter 10] Dynamic Programming_ , if you are interested, you can take a look. Recently, something happened at home. I was busy and didn't have time to brush up the questions.

63. Different Paths II - LeetCode

A robot is positioned in the upper left corner of an mxn grid (the starting point is labeled "Start" in the image below).

The robot can only move one step down or to the right at a time. The robot tries to reach the bottom right corner of the grid (marked "Finish" in the image below).

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.

 In fact, this kind of dynamic programming problem is still strictly in accordance with our dynamic planning four steps:

1. Determine the meaning of the dp array and the meaning of the following table

2. The push of the recursive formula

3. Initialization of the dp array

4. dp array traversal order

So step by step:

The meaning of dp[i][j] is the number of paths that can reach the position with coordinates (i, j)

Derivation formula: In essence, it is still dp[i][j] = dp[i-1][j] + dp[i][j-1]. However, some fine-tuning is required, because if there is an obstacle in this place, then you cannot pass it, or take the road that includes this point.

class Solution {
public:
    int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
           int m = obstacleGrid.size();
           int n = obstacleGrid[0].size();
        if (obstacleGrid[m - 1][n - 1] == 1 || obstacleGrid[0][0] == 1) 
        {
            return 0;
        }
             vector<vector<int>> dp(m, vector<int>(n, 0));
        for (int i = 0; i < m && obstacleGrid[i][0] == 0; i++)
        {
             dp[i][0] = 1;
        }
        
        for (int j = 0; j < n && obstacleGrid[0][j] == 0; j++) 
        {
            dp[0][j] = 1;
        }
        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n; j++) {
                if (obstacleGrid[i][j] == 1)
                {
                    continue;
                } 
                dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
            }
        }
        return dp[m - 1][n - 1];
    }
};

 343. Integer Split - LeetCode

Given a positive integer  n , split it into  k a  sum of positive integers  (  k >= 2 ), and maximize the product of these integers.

Return  the largest product you can get  .

class Solution {
public:
    int integerBreak(int n) {
        vector<int> dp(n + 1);
        dp[2] = 1;
        for (int i = 3; i <= n ; i++) {
            for (int j = 1; j <= i / 2; j++) {
                dp[i] = max(dp[i], max((i - j) * j, dp[i - j] * j));
            }
        }
        return dp[n];
    }
};

 Summarize:

        Great harvest today.

If my content is helpful to you, please like, comment and bookmark . Creation is not easy, everyone's support is my motivation to persevere!

Guess you like

Origin blog.csdn.net/fckbb/article/details/131924973