[LeetCode]62Unique Paths /63Unique Paths II/64Minimum Path Sum

版权声明:本文为博主原创文章,如要转载请在转载的文章后附上本人博客链接! https://blog.csdn.net/syyyy712/article/details/81699866

62. Unique Paths题目描述:

A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish’ in the diagram below).

How many possible unique paths are there?
这里写图片描述

Above is a 7 x 3 grid. How many possible unique paths are there?

Note: m and n will be at most 100.

Example 1:

Input: m = 3, n = 2
Output: 3
Explanation:
From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
1. Right -> Right -> Down
2. Right -> Down -> Right
3. Down -> Right -> Right

Example 2:

Input: m = 7, n = 3
Output: 28

解释:

此题用到动态规划,把结果存在二维数组中,状态转移方程为dp[k][t] = dp[k-1][t]+dp[k][t-1]。为了方便理解,我把例1的二维数组画出来如下:
这里写图片描述

C++代码如下:

int uniquePaths(int m, int n) 
    {
        int dp[1000][1000];
        dp[0][0]=1;
        for(int i = 1;i<m;i++)
        {
            dp[0][i]=1;
        }
        for(int j = 1;j<n;j++)
        {
            dp[j][0]=1;
        }
        for(int k = 1;k<n;k++)
        {
            for(int t = 1;t<m;t++)
            {
                dp[k][t] = dp[k-1][t]+dp[k][t-1];
            }
        }
        return dp[n-1][m-1];
    } 
};


63. Unique Paths ||题目描述:

A robot is located at the top-left corner of a m x n grid (marked ‘Start’ in the diagram below).

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked ‘Finish’ in the diagram below).

Now consider if some obstacles are added to the grids. How many unique paths would there be?
这里写图片描述
An obstacle and empty space is marked as 1 and 0 respectively in the grid.

Example 1:
Input:
[
[0,0,0],
[0,1,0],
[0,0,0]
]
Output: 2
Explanation:
There is one obstacle in the middle of the 3x3 grid above.
There are two ways to reach the bottom-right corner:
1. Right -> Right -> Down -> Down
2. Down -> Down -> Right -> Right

解释:

这道题目是上面那道题目的升级版,主要在格子某几个位置放了障碍物,所以放了障碍物说明这个格子的路径数为0,因为走不通,所以以例一为例二维动态数组表示如下:
这里写图片描述

C++代码:

class Solution {
public:
    int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) 
    {
        int rows = obstacleGrid.size();
        int columns = obstacleGrid[0].size();
        int dp[1000][1000]={0};
        for(int i = 0;i<rows;i++)
        {
            for(int j = 0;j<columns;j++)
            {
                if(obstacleGrid[i][j]==1)
                {
                    dp[i][j]=0;
                }
                else if(i==0 &&j==0)
                {
                    dp[i][j]=1;
                }
                else if(i==0 && j>0)
                {
                    dp[i][j]=dp[i][j-1];
                }
                else if(i>0 && j==0)
                {
                    dp[i][j] = dp[i-1][j];
                }
                else
                {
                    dp[i][j]=dp[i-1][j]+dp[i][j-1];
                }
            }
        }
        return dp[rows-1][columns-1];
    }
};


64. Minimum Path Sum题目描述:

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.

Note: You can only move either down or right at any point in time.

Example:

Input:
[
[1,3,1],
[1,5,1],
[4,2,1]
]
Output: 7
Explanation: Because the path 1→3→1→1→1 minimizes the sum.

解释:

此题也用到了动态规划,主要的过程还是填充二维数组dp,状态转移方程为dp[i][j]=grid[i][j]+min(dp[i-1][j],dp[i][j-1]),以例题为例,表示如下:
这里写图片描述

C++代码:

class Solution {
public:
    int minPathSum(vector<vector<int>>& grid) 
    {
        int m = grid.size();
        int n = grid[0].size();
        int dp[m][n];
        dp[0][0]=grid[0][0];
        for(int i = 1;i<m;i++)
        {
            dp[i][0] = grid[i][0]+dp[i-1][0];
        }
        for(int i = 1;i<n;i++)
        {
            dp[0][i] = grid[0][i]+dp[0][i-1];
        }
        for(int i = 1;i<m;i++)
        {
            for(int j = 1;j<n;j++)
            {
                dp[i][j] = grid[i][j]+min(dp[i-1][j],dp[i][j-1]);
            }
        }
        return dp[m-1][n-1];
    }
};

猜你喜欢

转载自blog.csdn.net/syyyy712/article/details/81699866
今日推荐