leetcode 62. Unique Paths (dp)
2017-09-18 16:50
549 查看
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?
思路分析:这题开始想到用DFS 搜索来做,一般搜索从S到F的路径一边count路径的数量,但是算法时间复杂度太高,用DFS算法时间复杂度为O(2^(m+n)),是指数级复杂度
高中的组合数学的问题,
m*n的棋盘,一共需要走(m-1)+(n-1)步,向右走m-1步,向下走n-1步,这(m-1)+(n-1)步中,只要确定了哪些步向右,即同时确定了哪些步向下走,反之亦然。
答案即C(m+n-2,m-1)或C(m+n-2,n-1)
方法1: 以下为组合的代码:
public class Solution {
2 public int uniquePaths(int m, int n) {
3 double res = 1;
4 for (int i = 1; i <= n - 1; i++)
5 res *= ((double) (m + i - 1) / (double) i);
6 return (int) Math.round(res);
7 }
8 }
说明Note中提示m、n最大为100是有用的,即你计算阶乘时int会溢出的。
方法2:动态规划,定义一个二维数组 A[M]
,数组 A[m]
,从 A[0][0] 到 A[m-1][n-1] 有多少条路径
从左上开始依次计算每一行的值,最后返回 A[M-1][N-1]即可,递推方程是:
A[I][J]=A[I-1][J]+A[I][J-1]; A[0][0]=1, 由此第一行的 a[i][0],第一列a[0][i]的 都为1
代码如下:
public int uniquePaths(int m, int n) {
// DP with 2 dimensions array
int[][] a = new int[m]
;
for (int i = 0; i < m; i++) {
a[i][0] = 1;
}
for (int i = 0; i < n; i++) {
a[0][i] = 1;
}
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
a[i][j] = a[i-1][j] + a[i][j-1];
}
}
return a[m-1][n-1];
}
问题延伸
与 【LeetCode】Unique
Paths 解题报告 不同的是,数组中多了障碍,其实只要把障碍地方的解变为 0 就好
public class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int rows = obstacleGrid.length;
if (rows < 1) return 0;
int cols = obstacleGrid[0].length;
if (cols < 1) return 0;
if (obstacleGrid[0][0] == 1 || obstacleGrid[rows - 1][cols - 1] == 1) return 0;
int[] ans = new int[cols];
ans[0] = 1;
for (int j = 1; j < cols; j++) {
if (obstacleGrid[0][j] == 0) {
ans[j] = ans[j - 1];
} else {
ans[j] = 0;
}
}
for (int i = 1; i < rows; i++) {
if (obstacleGrid[i][0] == 1) {
ans[0] = 0;
}
for (int j = 1; j < cols; j++) {
if (obstacleGrid[i][j] == 1) {
ans[j] = 0;
} else {
ans[j] += ans[j - 1];
}
}
}
return ans[cols - 1];
}
}
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?
思路分析:这题开始想到用DFS 搜索来做,一般搜索从S到F的路径一边count路径的数量,但是算法时间复杂度太高,用DFS算法时间复杂度为O(2^(m+n)),是指数级复杂度
高中的组合数学的问题,
m*n的棋盘,一共需要走(m-1)+(n-1)步,向右走m-1步,向下走n-1步,这(m-1)+(n-1)步中,只要确定了哪些步向右,即同时确定了哪些步向下走,反之亦然。
答案即C(m+n-2,m-1)或C(m+n-2,n-1)
方法1: 以下为组合的代码:
public class Solution {
2 public int uniquePaths(int m, int n) {
3 double res = 1;
4 for (int i = 1; i <= n - 1; i++)
5 res *= ((double) (m + i - 1) / (double) i);
6 return (int) Math.round(res);
7 }
8 }
说明Note中提示m、n最大为100是有用的,即你计算阶乘时int会溢出的。
方法2:动态规划,定义一个二维数组 A[M]
,数组 A[m]
,从 A[0][0] 到 A[m-1][n-1] 有多少条路径
从左上开始依次计算每一行的值,最后返回 A[M-1][N-1]即可,递推方程是:
A[I][J]=A[I-1][J]+A[I][J-1]; A[0][0]=1, 由此第一行的 a[i][0],第一列a[0][i]的 都为1
代码如下:
public int uniquePaths(int m, int n) {
// DP with 2 dimensions array
int[][] a = new int[m]
;
for (int i = 0; i < m; i++) {
a[i][0] = 1;
}
for (int i = 0; i < n; i++) {
a[0][i] = 1;
}
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
a[i][j] = a[i-1][j] + a[i][j-1];
}
}
return a[m-1][n-1];
}
问题延伸
与 【LeetCode】Unique
Paths 解题报告 不同的是,数组中多了障碍,其实只要把障碍地方的解变为 0 就好
public class Solution {
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
int rows = obstacleGrid.length;
if (rows < 1) return 0;
int cols = obstacleGrid[0].length;
if (cols < 1) return 0;
if (obstacleGrid[0][0] == 1 || obstacleGrid[rows - 1][cols - 1] == 1) return 0;
int[] ans = new int[cols];
ans[0] = 1;
for (int j = 1; j < cols; j++) {
if (obstacleGrid[0][j] == 0) {
ans[j] = ans[j - 1];
} else {
ans[j] = 0;
}
}
for (int i = 1; i < rows; i++) {
if (obstacleGrid[i][0] == 1) {
ans[0] = 0;
}
for (int j = 1; j < cols; j++) {
if (obstacleGrid[i][j] == 1) {
ans[j] = 0;
} else {
ans[j] += ans[j - 1];
}
}
}
return ans[cols - 1];
}
}
相关文章推荐
- [LeetCode] 62. Unique Paths (DP问题)
- LeetCode 62. Unique Paths
- LeetCode||62. Unique Paths
- Leetcode 62. Unique Paths
- [LeetCode] Algorithms-62. Unique Paths
- [leetcode][DP] Unique Paths II
- [LeetCode] [C++] 62. Unique Paths
- LeetCode 62. Unique Paths(java)
- leetcode.array--62. Unique Paths
- leetcode 62. Unique Paths && leetcode 63. Unique Paths II
- LeetCode 62. Unique Paths
- leetcode题解-561. Array Partition I && 62. Unique Paths && 63. Unique Paths II
- LeetCode Unique Paths II(dp)
- Leetcode-62. Unique Paths
- LeetCode Unique Paths (简单DP)
- [LeetCode]62. Unique Paths
- leetcode 62. Unique Paths
- [LeetCode] 62. Unique Paths
- Leetcode 62. Unique Paths
- leetcode 62. Unique Paths