62. Unique Paths
题目:
A robot is located at the top-left corner of a m x ngrid (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.
链接: http://leetcode.com/problems/unique-paths/
题解:
dp的经典问题,每次向右或向下走一步。第一行或者第一列走到头只有一种方法,所以初始化为1,转移方程是dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
Time Complexity O(m * n), Space Complexity O(m * n)。
public class Solution {
public int uniquePaths(int m, int n) {
int[][] dp = new int[m][n]; for(int i = 0; i < m; i ++)
dp[i][0] = 1; for(int j = 0; j < n; j ++)
dp[0][j] = 1; for(int i = 1; i < m; i ++){
for(int j = 1; j < n; j++){
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
} return dp[m - 1][n - 1];
}
}
Update:
public class Solution {
public int uniquePaths(int m, int n) {
if(m == 0 || m == 0)
return 0;
int[][] dp = new int[m][n]; for(int i = 0; i < m; i++) // initialize first column
dp[i][0] = 1; for(int j = 1; j < n; j++) // initialize first row
dp[0][j] = 1; for(int i = 1; i < m; i++) {
for(int j = 1; j < n; j++) {
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
} return dp[m - 1][n - 1];
}
}
二刷:
Java:
经典的dp,据说还可以用Math来做。我们还是使用dp。
2D DP:
建立一个m x n矩阵,初始化第一条边和第一列为1,然后利用转移方程res[i][j] = res[i - 1][j] + res[i][j - 1],最后返回res[m - 1][n - 1]
Time Complexity - O(mn), Space Complexity - O(mn)
public class Solution {
public int uniquePaths(int m, int n) {
if (m < 0 || n < 0) {
return 0;
}
int[][] res = new int[m][n];
for (int i = 0; i < m; i++) {
res[i][0] = 1;
}
for (int j = 1; j < n; j++) {
res[0][j] = 1;
}
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
res[i][j] = res[i - 1][j] + res[i][j - 1];
}
}
return res[m - 1][n - 1];
}
}
1D DP with rolling array:
对这种简单的DP,一般我们可以用rolling array来减少空间复杂度。我们建立一个长度为n的array,先初始化其中每个元素的值为1,然后在遍历m x n的时候,转移方程简化为 res[j] += res[j - 1], 还是之前res[i][j]左边和上边的元素。这样节约了一点空间。
Time Complexity - O(mn), Space Complexity - O(n)
public class Solution {
public int uniquePaths(int m, int n) {
if (m < 0 || n < 0) {
return 0;
}
int[] res = new int[n];
for (int j = 0; j < n; j++) {
res[j] = 1;
}
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
res[j] += res[j - 1];
}
}
return res[n - 1];
}
}
三刷:
Java:
2D dp:
public class Solution {
public int uniquePaths(int m, int n) {
if (m < 0 || n < 0) return 0;
int[][] dp = new int[m][n];
for (int i = 0; i < m; i++) dp[i][0] = 1;
for (int j = 1; j < n; j++) dp[0][j] = 1; for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
}
return dp[m - 1][n - 1];
}
}
Rolling array 1:
public class Solution {
public int uniquePaths(int m, int n) {
if (m < 0 || n < 0) return 0;
int[] dp = new int[n];
for (int j = 0; j < n; j++) dp[j] = 1; for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
dp[j] += dp[j - 1];
}
}
return dp[n - 1];
}
}
Rolling array2: 现在才能领会到为什么我们有的时候建立dp数组要用int[] dp = new int[n + 1]。 多增加一个长度的话是为了写的时候不用对第一行赋初值,看起来比较简练,但其实时间复杂度还是一样的。
public class Solution {
public int uniquePaths(int m, int n) {
if (m < 0 || n < 0) return 0;
int[] dp = new int[n + 1];
dp[0] = 1; for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
dp[j] += dp[j - 1];
}
}
return dp[n - 1];
}
}
Reference:
https://leetcode.com/discuss/9110/my-ac-solution-using-formula
https://leetcode.com/discuss/47829/math-solution-o-1-space
62. Unique Paths的更多相关文章
- leetcode 62. Unique Paths 、63. Unique Paths II
62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...
- 刷题62. Unique Paths
一.题目说明 题目62. Unique Paths,在一个m*n矩阵中,求从左上角Start到右下角Finish所有路径.其中每次只能向下.向右移动.难度是Medium! 二.我的解答 这个题目读读题 ...
- [LeetCode] 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 ...
- 62. Unique Paths && 63 Unique Paths II
https://leetcode.com/problems/unique-paths/ 这道题,不利用动态规划基本上规模变大会运行超时,下面自己写得这段代码,直接暴力破解,只能应付小规模的情形,当23 ...
- LeetCode OJ 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 ...
- LeetCode 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 ...
- 62. Unique Paths(中等,我自己解出的第一道 DP 题^^)
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...
- 【一天一道LeetCode】#62. Unique Paths
一天一道LeetCode系列 (一)题目 A robot is located at the top-left corner of a m x n grid (marked 'Start' in th ...
- [leetcode]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 ...
随机推荐
- C# new的用法
在 C# 中,new 关键字可用作运算符.修饰符或约束. 1)new 运算符:用于创建对象和调用构造函数.这种大家都比较熟悉,没什么好说的了. 2)new 修饰符:在用作修饰符时,new 关键字可以显 ...
- Redis 代理服务Twemproxy
1.twemproxy explore 当我们有大量 Redis 或 Memcached 的时候,通常只能通过客户端的一些数据分配算法(比如一致性哈希),来实现集群存储的特性.虽然Redis 2.6版 ...
- backbone前端基础框架搭建
前端站点名为:site: 前端框架分为:css.js和img,框架的核心在js文件夹下: js中包括collections.models.views.lib和一个app入口js
- SpringMVC Cache注解+Redis
依赖jar包:Xml代码 收藏代码 <!-- redis --> <dependency> <groupId ...
- C#: Create a WebRequest with HTTP Basic Authentication
http://blog.csdn.net/huangyaoshifog/article/details/4470675 myReq = WebRequest.Create(url); string u ...
- 3563: DZY Loves Chinese - BZOJ
Description神校XJ之学霸兮,Dzy皇考曰JC.摄提贞于孟陬兮,惟庚寅Dzy以降.纷Dzy既有此内美兮,又重之以修能.遂降临于OI界,欲以神力而凌♂辱众生. 今Dzy有一魞歄图,其上有N座祭 ...
- 1067: [SCOI2007]降雨量 - BZOJ
Description 我们常常会说这样的话:“X年是自Y年以来降雨量最多的”.它的含义是X年的降雨量不超过Y年,且对于任意Y<Z<X,Z年的降雨量严格小于X年.例如2002,2003,2 ...
- EXT经验--查询items的xtype
前言:EXT由多个组件组成,每个组件可配置多个子组件(items),而每个子组件也可嵌套多个子组件(items)--给人一种子子孙孙无穷匮也的印象,这对于初学者引来一个很重要的问题,特别是阅读他人编写 ...
- PAT-乙级-1055. 集体照 (25)
1055. 集体照 (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 拍集体照时队形很重要,这里对给定的N ...
- 使用静态变量的方法求n!
下面的程序可以输出1-5的阶乘值,如果需要把5改为n,则可求出1-n的阶乘值. void main() { setvbuf(stdout,NULL,_IONBF,); int fac(int n); ...