题目描述

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.

根据题意分析,我们得出一个重要的结论:
  机器人走到终点的所有路径 = 机器人走到1位置的所有路径 +机器人走到2位置的所有路径。
同理如果要求走到1位置的所有路径,只要求它上面和左面的所有路径之和。

 

再来看当被划红线的小方块作为终点时,都只有一条唯一的路径。

很明显我们可以用动态规划来解决这个问题。经过上面的分析后,可以列出状态转义方程:

dp[][j] =
dp[i][] =
dp[i][j] = dp[i - ][j] + dp[i][j - ]

代码如下:

class Solution {
public:
int uniquePaths(int m, int n) {
int dp[m][n];
for (int i = ; i < m; i++) {
dp[i][] = ;
}
for (int j = ; j < n; j++) {
dp[][j] = ;
}
for (int i = ; i < m; i++) {
for (int j = ; j < n; j++) {
dp[i][j] = dp[i - ][j] + dp[i][j - ];
}
}
return dp[m-][n-];
}
};

使用动态规划时间复杂度只需要O(m*n)。在求解最优化问题时,无非最常用的就是贪心和动态规划两种。在使用动态规划中,先对问题仔细分析,列出状态转移方程以及边界条件,接下来代码就是水到渠成的事情了。

Unique-paths (动态规划)的更多相关文章

  1. LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]

    唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...

  2. [LeetCode] Unique Paths && Unique Paths II && Minimum Path Sum (动态规划之 Matrix DP )

    Unique Paths https://oj.leetcode.com/problems/unique-paths/ A robot is located at the top-left corne ...

  3. 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance

    引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...

  4. Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II)

    Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II) 初级题目:Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机 ...

  5. Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths)

    Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ). 机器人每次只能向下或者向 ...

  6. Leetcode 动态规划 Unique Paths

    本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie Unique Paths Total Accepted: 17915 Total Submi ...

  7. LeetCode之“动态规划”:Minimum Path Sum && Unique Paths && Unique Paths II

    之所以将这三道题放在一起,是因为这三道题非常类似. 1. Minimum Path Sum 题目链接 题目要求: Given a m x n grid filled with non-negative ...

  8. 63. Unique Paths II(有障碍的路径 动态规划)

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

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

  10. [LeetCode] Unique Paths II 不同的路径之二

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

随机推荐

  1. python之路--day15--常用模块之logging模块

    常用模块 1 logging模块 日志级别:Noset (不设置) Debug---(调试信息)----也可用10表示 Info--(消息信息)----也可用20表示 Warning---(警告信息) ...

  2. csrf学习笔记

    CSRF全称Cross Site Request Forgery,即跨站点请求伪造.我们知道,攻击时常常伴随着各种各样的请求,而攻击的发生也是由各种请求造成的. CSRF攻击能够达到的目的是使受害者发 ...

  3. 记一次oracle crs无法重启事故

    今天在修改了数据库参数后,关闭数据库及crs,然后重新启动了服务器,服务器启动完成之后,发现数据库无法启动,过程如下: step1:重启数据库 $ su - grid $ srvctl stop da ...

  4. Python内置函数(16)——ord

    英文文档: ord(c) Given a string representing one Unicode character, return an integer representing the U ...

  5. Python-字符串及列表操作-Day2

    1.数据类型 1.1 变量引出数据类型 变量:用来记录状态变量值的变化就是状态的变化,程序运行的本质就是来处理一系列的变化 1.2 五大基本数据类型: 数字 字符串 列表 元组 字典 1.2.1 数字 ...

  6. 新概念英语(1-57)An unusual day

    新概念英语(1-57)An unusually day What is Mr. Sawyer doing tonight? It is eight o'clock. The children go t ...

  7. Tomcat(1-1)重置Tomcat8.5管理员的用户名和密码

    1.访问 http://localhost:8080/,点击 [manager app],提示输入用户名和密码,admin/admin后报错.  2.解决办法:重置Tomcat8.5管理员的用户名和密 ...

  8. MySql入门(2-2)创建数据库

    mysql -u root -p; show databases; create database apigateway; use apigateway; show tables;

  9. GIT入门笔记(20)- git 开发提交代码过程梳理

    git开发提交流程新项目开发,可以直接往master上提交老项目维护,可以在分支上修改提交,多次add和commit之后,也可以用pull合并主干和本地master,解决冲突后再push 1.检出代码 ...

  10. Mysql启动时提示:Another MySQL daemon already running with the same unix socket.

    场景:vmvare虚拟机.centos7.mysql5.7 解决: mv /var/lib/mysql/mysql.sock /var/lib/mysql/mysql.sock.bak 参考: htt ...