题目:

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. Oracle 10g 数据文件的第一个数据块结构

    一.数据文件的第一个数据块结构kcvfh BBED> set file 1 FILE# 1 BBED> set block 1 BLOCK# 1 --查看第一个数据块的整体结构 BBED& ...

  2. Python计算斗牛游戏的概率

    Python计算斗牛游戏的概率 过年回家,都会约上亲朋好友聚聚会,会上经常会打麻将,斗地主,斗牛.在这些游戏中,斗牛是最受欢迎的,因为可以很多人一起玩,而且没有技术含量,都是看运气(专业术语是概率). ...

  3. flex 监听网络连接情况

    NativeApplication.nativeApplication.addEventListener(Event.NETWORK_CHANGE, onNetworkChange); private ...

  4. 用Redis bitmap统计活跃用户、留存

    Spool的开发者博客,描述了Spool利用Redis的bitmaps相关的操作,进行网站活跃用户统计工作. 原文:http://blog.getspool.com/2011/11/29/fast-e ...

  5. Android--消除“Permission is only granted to system apps”错误

    原文:http://blog.csdn.net/gaojinshan/article/details/14230673 在AndroidManifest.xml中使用了如下的配置: <uses- ...

  6. linux内核中的get_user和put_user

    linux内核中的get_user和put_user 在 内核空间和用户空间交换数据时,get_user和put_user是两个两用的函数.相对于copy_to_user和 copy_from_use ...

  7. VBS基础篇 - FileSystemObject对象

    文件系统是所有操作系统最重要的部分之一,脚本经常会需要对文件及文件夹进行访问和管理,在Vbs中对桌面和文件系统进行访问的顶级对象是FileSystemObject FSO包含的常见对象有:       ...

  8. .net 科学类型相关问题

    Q:如果我要把使用科学记数法表示的string转换为int又该如何呢? A:你可以通过把NumberStyles.AllowDecimalPoint | NumberStyles.AllowExpon ...

  9. Document.defaultView

    Document.defaultView 引子 最近愚安在写一个可以将页面上的资源链接转为二维码以方便移动端浏览的chrome插件,由于dom操作并不多,而且作为插件不需要考虑跨 浏览器兼容性,所以并 ...

  10. 3142:[HNOI2013]数列 - BZOJ

    题目描述 Description 小T最近在学着买股票,他得到内部消息:F公司的股票将会疯涨. 股票每天的价格已知是正整数,并且由于客观上的原因,最多只能为N.在疯涨的K天中小T观察到:除第一天外每天 ...