题目:

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的更多相关文章

  1. leetcode 62. Unique Paths 、63. Unique Paths II

    62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...

  2. 刷题62. Unique Paths

    一.题目说明 题目62. Unique Paths,在一个m*n矩阵中,求从左上角Start到右下角Finish所有路径.其中每次只能向下.向右移动.难度是Medium! 二.我的解答 这个题目读读题 ...

  3. [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 ...

  4. 62. Unique Paths && 63 Unique Paths II

    https://leetcode.com/problems/unique-paths/ 这道题,不利用动态规划基本上规模变大会运行超时,下面自己写得这段代码,直接暴力破解,只能应付小规模的情形,当23 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 【一天一道LeetCode】#62. Unique Paths

    一天一道LeetCode系列 (一)题目 A robot is located at the top-left corner of a m x n grid (marked 'Start' in th ...

  9. [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 ...

随机推荐

  1. 新手自学ABAP(1)--数据类型

    一.DATA语句 1.TYPE type ex:  可以利用冒号声明多个变量. DATA : gv_num1 TYPE I,   gv_num2 TYPE I. 2.LIKE num   (num可以 ...

  2. hdu 5738 2016 Multi-University Training Contest 2 Eureka 计数问题(组合数学+STL)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5738 题意:从n(n <= 1000)个点(有重点)中选出m(m > 1)个点(选出的点只 ...

  3. ActiveMQ之selector的用法

    前面的例子中创建一个消息消费者使用的是: sesssion.createConsumer(destination) 另外,还提供了另一种方式: sesssion.createConsumer(dest ...

  4. nodejs fs 模块的用途

    /*** New node filefs 操作*/var fs = require(“fs”); /*创建文件 var fileName = “anps_hsj”;fs.mkdir(fileName, ...

  5. iOS:等比压缩截图代码

    将一幅图片按着需要的尺寸进行等比的压缩和放大,最后再截取需要尺寸部分,不知道说清楚没,反正就那意思吧! +(UIImage *)compressImageWith:(UIImage *)image w ...

  6. centos 格式化硬盘并挂载,添加重启后生效

    [root@cloud /]# passwd 更改用户 root 的密码 . 新的 密码: 重新输入新的 密码: passwd: 所有的身份验证令牌已经成功更新. [root@cloud /]# fd ...

  7. 【转载】使用Axure制作App原型怎样设置尺寸?

    使用Axure制作App原型怎样设置尺寸? 原文地址:http://www.axure.us/2172/ 本文由原型库网站投稿,转载请注明出处. 最近有几位小伙伴儿都提出同样一个疑问:想用Axure设 ...

  8. android 中设置HttpURLConnection 超时并判断是否超时

    设置超时: URL url1 = new URL(url); HttpURLConnection conn = (HttpURLConnection) url1.openConnection(); c ...

  9. IIS搭建本地服务器,花生壳实现外网通过域名访问网站

    配置服务器 作为一个青年,没有实力,做不出标图所示的服务器. 作为一个学生,买不起服务器 作为一个小孩,买不起域名 但别忘了 作为一个平民玩家,只要有耐心 装备迟早会做出来的 (注:感觉有钱与没钱还是 ...

  10. 阿旭的php开发环境

    过了几年,php技术也日新月异,变化也挺多.哪么对于开发者,感觉有一些简单的方法,写下来,以备以后使用.我觉得吧,Linux写php不如windows写php,调试,查资料和各种功能比较全面,而lin ...