现在的位置: 首页 > 综合 > 正文

[LeetCode] Unique Paths、Unique Paths II、Minimum Path Sum

2018年04月12日 ⁄ 综合 ⁄ 共 2328字 ⁄ 字号 评论关闭

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 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) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        
        vector<vector<int> > grid(m,vector<int>(n,0));
	    for(int i=0;i<m;i++)
		    grid[i][0]=1;
	    for(int i=0;i<n;i++)
		    grid[0][i]=1;
	    for(int i=1;i<m;i++)
		    for(int j=1;j<n;j++)
			    grid[i][j]=grid[i-1][j]+grid[i][j-1];
	    return grid[m-1][n-1];
    }
};

Unique Paths II:

Follow up for "Unique Paths":

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> > &grid) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        
        int m=grid.size();
	    if ( m==0 )
		    return 0;
	    int n = grid[0].size();
	    for(int i=0;i<m;i++)
		    assert(grid[i].size()==n);
	    if ( grid[0][0]== 1 )
		    return 0;

	    vector<vector<int> > path(m,vector<int>(n,0));
	    path[0][0]=1;
	    for(int i=1;i<m;i++)
	    {
		    if ( grid[i][0]==0 && path[i-1][0]==1)
			    path[i][0]=1;
		    else
			    path[i][0]=0;
	    }
	    for(int j=1;j<n;j++)
	    {
		    if (grid[0][j]==0 &&path[0][j-1]==1)
			    path[0][j]=1;
		    else
			    path[0][j]=0;
	    }
	    for(int i=1;i<m;i++)
	    {
		    for(int j=1;j<n;j++)
		    {
			    if( grid[i][j]==1 )
				    path[i][j]=0;
			    else
			    {
				    path[i][j]=path[i-1][j]+path[i][j-1];
			    }
		    }
	    }
	    return path[m-1][n-1];
    }
};

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.

class Solution {
public:
    int minPathSum(vector<vector<int> > &grid) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        
        int m=grid.size();
	    if ( m==0 )
		    return 0;
	    int n = grid[0].size();
	    for(int i=0;i<m;i++)
		    assert(grid[i].size()==n);

	    vector<vector<int> > sum(m,vector<int>(n,0));
	    sum[0][0]=grid[0][0];
        
	    for(int i=1;i<m;i++)
		    sum[i][0]=sum[i-1][0]+grid[i][0];
	    for(int i=1;i<n;i++)
		    sum[0][i]=sum[0][i-1]+grid[0][i];
	    for(int i=1;i<m;i++)
		    for(int j=1;j<n;j++)
			    sum[i][j]=min(sum[i-1][j],sum[i][j-1])+grid[i][j];
	    return sum[m-1][n-1];

    }
};

抱歉!评论已关闭.