62. 63. Unique Paths 64. Minimum Path Sum

1.

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 3 x 7 grid. How many possible unique paths are there?

Note: m and n will be at most 100.

class Solution {
public:
    int uniquePaths(int m, int n) {
        vector<vector<int>> v(m, vector<int>(n, 0));
        int i, j;
        for(i = 0; i < m; i++)
        {
            for(j = 0; j < n; j++)
            {
                if(0 == i || 0 == j)
                    v[i][j] = 1;
                else
                    v[i][j] = v[i-1][j] + v[i][j-1];
            }
        }
        return v[m-1][n-1];
    }
};

2.

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.

For example,

There is one obstacle in the middle of a 3x3 grid as illustrated below.

[
  [0,0,0],
  [0,1,0],
  [0,0,0]
]

The total number of unique paths is 2.

Note: m and n will be at most 100.

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

3.

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.

int minPathSum(vector<vector<int> > &grid) {
    if (grid.size()<=0){
        return 0;
    }
    int i, j;
    for(i=0; i<grid.size(); i++){
        for(j=0; j<grid[i].size(); j++){
            int top = i-1<0 ? INT_MAX : grid[i-1][j] ;
            int left = j-1<0 ? INT_MAX : grid[i][j-1];
            if (top==INT_MAX && left==INT_MAX){
                continue;
            }
            grid[i][j] += (top < left? top: left);
        }
    }
    return grid[grid.size()-1][grid[0].size()-1];
}
时间: 2024-07-30 10:18:08

62. 63. Unique Paths 64. Minimum Path Sum的相关文章

[LeetCode] Unique Paths &amp;&amp; Unique Paths II &amp;&amp; Minimum Path Sum (动态规划之 Matrix DP )

Unique Paths https://oj.leetcode.com/problems/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 rea

&amp;lt;LeetCode OJ&amp;gt; 62. / 63. Unique Paths(I / II)

62. Unique Paths My Submissions Question Total Accepted: 75227 Total Submissions: 214539 Difficulty: Medium 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

LeetCode --- 64. Minimum Path Sum

题目链接: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. 这道题的要求是在m*n

LeetCode 62, 63 Unique Paths I+II

62. Unique Paths 空间可以按行优化为 O(n),也可以按列优化为O(m). class Solution { public: int uniquePaths(int m, int n) { int dp[m][n]={0}; dp[0][0]=1; for (int i=0;i<m;++i){ for (int j=0;j<n;++j){ if (i==0&&j==0) continue; dp[i][j]=0; if (j-1>=0) dp[i][j]

[LC] 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

LeetCode 64. Minimum Path Sum 20170515

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. 题目大意:给定一个一个m*n格的方针,每个方格都放一个非负数,找到

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. =============== 思路:经典DP动态规划入门问题,

64. Minimum Path Sum (Graph; DP)

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. class Solution { public: int minP

LeetCode 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. 题目标签:Array 这道题目和前面两题差不多,基本思想都是一样的