这三个问题及其类似,背景都是和路径相关,方法都是动态优化
目录
62题:
题目描述:
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
算法实现:
维护一个n*m的数组status,在相应位置(i,j)存储从(0,0)到(i,j)的方法总数;
对于m=0和n=0的一列一行,方法数都是1;
递归公式为status[i][j] = status[i - 1][j] + status[i][j - 1]
// 递归法,超时了
// class Solution {
// public:
// int uniquePaths(int m, int n) {
// if(m == 1 || n == 1)return 1;
// else return uniquePaths(m - 1, n) + uniquePaths(m, n - 1);
// }
// };
//动态规划
class Solution {
public:
int uniquePaths(int m, int n) {
vector<vector<int>> status(n,vector<int>(m));
status[0][0] = 1;
for(int i = 1; i < m; i ++)
status[0][i] = 1;
for(int i = 1; i < n; i ++)
status[i][0] = 1;
for(int i = 1; i < n; i ++)
for(int j = 1; j < m; j ++)
status[i][j] = status[i - 1][j] + status[i][j - 1];
return status[n - 1][m - 1];
}
};
63题
题目描述:
Now consider if some obstacles are added to the grids. How many unique paths would there be?
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
算法实现:
递归公式和62有所不同,如果当前位置有障碍物,则status[i][j]清零;同时m=0, n=0的一行一列也是如此,如果当前位置之前有障碍物则数组值为零,否则为1,程序类似
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
int n = obstacleGrid.size();
int m = obstacleGrid[0].size();
vector<vector<int>> status(n,vector<int>(m));
if(obstacleGrid[0][0] == 1)status[0][0] = 0;
else status[0][0] = 1;
for(int i = 1; i < m; i ++)
{
if(obstacleGrid[0][i] == 1)status[0][i] = 0;
else status[0][i] = status[0][i - 1];
}
for(int i = 1; i < n; i ++)
{
if(obstacleGrid[i][0] == 1)status[i][0] = 0;
else status[i][0] = status[i - 1][0];
}
for(int i = 1; i < n; i ++)
for(int j = 1; j < m; j ++)
if(obstacleGrid[i][j] == 1)status[i][j] = 0;
else status[i][j] = status[i - 1][j] + status[i][j - 1];
return status[n - 1][m - 1];
}
};
64题
题目描述
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.
算法实现
这个题目则是对每个节点处加了权值,要找一个权值最小的路径,那么当前status数组就是维护当前的最短路径
对于m=0和n=0的一列一行只需要将当前位置之前的元素相加得到的值放入status数组即可;
对于其他递归公式为
status[i][j] = min(status[i - 1][j] , status[i][j - 1]) + grid[i][j];
class Solution {
public:
int minPathSum(vector<vector<int>>& grid) {
int n = grid.size();
int m = grid[0].size();
vector<vector<int>> status(n,vector<int>(m));
status[0][0] = grid[0][0];
for(int i = 1; i < m; i ++)
status[0][i] = grid[0][i] + status[0][i - 1];
for(int i = 1; i < n; i ++)
status[i][0] = grid[i][0] + status[i - 1][0];
for(int i = 1; i < n; i ++)
for(int j = 1; j < m; j ++)
status[i][j] = min(status[i - 1][j] , status[i][j - 1]) + grid[i][j];
return status[n - 1][m - 1];
}
};