1. 程式人生 > >[LeetCode]62Unique Paths /63Unique Paths II/64Minimum Path Sum

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

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];
    }
};