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 3 x 7 grid. How many possible unique paths are there?
Note: m and n will be at most 100.
一个机器人到右下角摘星星,只能向右向下移动,问有多少条路可达。
一个dp题,每一个格子可从他的上边或者左边走过来,那么到达这个格子的路径数 = 到达他上边格子的路径数 + 到达他左边格子的路径数。时间复杂度O(m*n),空间复杂度O(m*n)
class Solution {
public:
int uniquePaths(int m, int n) {
vector<vector<int> > ans(m, vector<int>(n, 0));
for (int i = 0; i < m; ++i)
ans[i][0] = 1;
for (int i = 0; i < n; ++i)
ans[0][i] = 1;
for (int i = 1; i < m; ++i) {
for (int j = 1; j < n; ++j) {
ans[i][j] = ans[i-1][j] + ans[i][j-1];
}
}
return ans[m-1][n-1];
}
};
看到一种写法,回来补下题解,上面那种空间复杂度过高,可以降
因为每次只需要用到上一行的值,所以会发现我们只要开两个vector存上一行的值和这一行的值便足够了,不需要开整个二维空间存放。
class Solution {
int uniquePaths(int m, int n) {
if (m > n) return uniquePaths(n, m);
vector<int> pre(m, 1);
vector<int> cur(m, 1);
for (int j = 1; j < n; j++) {
for (int i = 1; i < m; i++)
cur[i] = cur[i - 1] + pre[i];
swap(pre, cur);
}
return pre[m - 1];
}
};
然后发现,我们每次都覆盖了pre的值,就相当于,只用到了cur上一次存的值,这个时候我们可以省下pre数组的空间,只存cur即可。此时时间复杂度O(mn),空间复杂度(max(n,m))
class Solution {
int uniquePaths(int m, int n) {
if (m > n) return uniquePaths(n, m);
vector<int> cur(m, 1);
for (int j = 1; j < n; j++)
for (int i = 1; i < m; i++)
cur[i] += cur[i - 1];
return cur[m - 1];
}
};
63. 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.
在上一题的基础上添加了障碍物,思路跟上题一样,就是处理dp的时候,将障碍物存在的点ans[i][j]设为0,其余不变。时间复杂度O(m*n),空间复杂度O(m*n)
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
int m = obstacleGrid.size();
int n = obstacleGrid[0].size();
vector<vector<int> > ans(m, vector<int>(n, 0));
for (int i = 0; i < m; ++i) {
if (obstacleGrid[i][0] == 1) break;
ans[i][0] = 1;
}
for (int i = 0; i < n; ++i) {
if (obstacleGrid[0][i] == 1) break;
ans[0][i] = 1;
}
for (int i = 1; i < m; ++i) {
for (int j = 1; j < n; ++j) {
if (obstacleGrid[i][j] == 1) continue;
ans[i][j] = ans[i-1][j] + ans[i][j-1];
}
}
return ans[m-1][n-1];
}
};
新写法,跟上一题第二种做法思路相同,时间复杂度O(mn),空间复杂度O(n)
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
int m = obstacleGrid.size();
int n = obstacleGrid[0].size();
vector<int> ans(n, 0);
if (obstacleGrid[0][0] == 0) ans[0] = 1;
for (int i = 0; i < m; ++i) {
if (obstacleGrid[i][0] == 1) ans[0] = 0;
for (int j = 1; j < n; ++j) {
if (obstacleGrid[i][j] == 1) ans[j] = 0;
else ans[j] += ans[j-1];
}
}
return ans[n-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.
求一条从左上角到右下角沿途数字和最小的路径。延续了上面两题的新思路,依旧是只开O(n)的空间,来存每一行的结果(因为每一行的结果仅与上一行有关)。时间复杂度O(mn),空间复杂度O(n)class Solution {
public:
int minPathSum(vector<vector<int>>& grid) {
int m = grid.size();
int n = grid[0].size();
vector<int> ans(n, 0);
ans[0] = grid[0][0];
for (int i = 1; i < n; ++i) {
ans[i] = ans[i-1] + grid[0][i];
}
for (int i = 1; i < m; ++i) {
ans[0] += grid[i][0];
for (int j = 1; j < n; ++j) {
ans[j] = min(ans[j-1], ans[j]) + grid[i][j];
}
}
return ans[n-1];
}
};
这篇博客介绍了LeetCode的62题和63题,涉及机器人从左上角到达右下角的独特路径数量。在62题中,机器人只能向右或向下移动,通过动态规划求解。63题在此基础上增加了障碍物,路径数量会受到影响。64题则要求找到一条路径,使得经过的所有数字之和最小。所有题目都采用动态规划策略,具有O(m*n)的时间复杂度和空间复杂度。
3万+

被折叠的 条评论
为什么被折叠?



