Unique-paths (动态规划)
题目描述
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 (动态规划)的更多相关文章
- LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]
唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...
- [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 ...
- 动态规划小结 - 二维动态规划 - 时间复杂度 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) 有关 这种情况下,时间 ...
- Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II)
Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II) 初级题目:Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机 ...
- Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths)
Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ). 机器人每次只能向下或者向 ...
- Leetcode 动态规划 Unique Paths
本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie Unique Paths Total Accepted: 17915 Total Submi ...
- LeetCode之“动态规划”:Minimum Path Sum && Unique Paths && Unique Paths II
之所以将这三道题放在一起,是因为这三道题非常类似. 1. Minimum Path Sum 题目链接 题目要求: Given a m x n grid filled with non-negative ...
- 63. Unique Paths II(有障碍的路径 动态规划)
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
- 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] Unique Paths II 不同的路径之二
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
随机推荐
- JAVA_SE基础——14.循环结构语句
建议有些基础的同学阅读,0基础可能会有些困难(最好看正文配合基础课本的例子) 所谓循环语句主要就是在满足条件的情况下反复执行某一个操作.Java提供了3种常用的循环语句,分别为for循环语句.whil ...
- python 基础 set 集合类型补充
为啥今天又重提这个数据类型呢?平时用的少,等要用起来的时候才发现,自己对这块啥都不知道了,so,今天就把这块再梳理一下咯. 一.set集合,是一个无序且不重复的元素集合.这一点是非常重要的. 二.集合 ...
- 新概念英语(1-133)Sensational news!
Lesson 133 Sensational news! 爆炸性新闻! Listen to the tape then answer this question. What reason did Ka ...
- Django 使用celery任务队列的配置
celery 情景:用户发起request,并等待response返回.在本些views中,可能需要执行一段耗时的程序,那么用户就会等待很长时间,造成不好的用户体验,比如发送邮件.手机验证码等. 使用 ...
- 翻译:JVM虚拟机规范1.7中的运行时常量池部分(一)
原文链接: https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.4 Java Virtual Machine i ...
- linux系统下的SVN安装
1.直接安装 # sudo apt-get install subversion 2. 创建版本库 # sudo mkdir /home/svn # sudo svnadmin create /hom ...
- 微信小程序开发-IP地址查询-例子
微信小程序开发 小程序搜索框 IP地址查询 搜索查询 样例 微信小程序 开发 参考 https://mp.weixin.qq.com/debug/wxadoc/dev/component/ ...
- html框架练习-基本网页制作
index.html <html> <head> <title>html框架</title> <meta charset="utf-8& ...
- Windows下Java开发环境安装与配置
1. 前往Oracle网站下载JDK程序并安装. http://www.oracle.com/technetwork/java/javase/downloads/index.html 目前最新的版本为 ...
- [LeetCode] 2 Keys Keyboard 两键的键盘
Initially on a notepad only one character 'A' is present. You can perform two operations on this not ...