LeetCode之动态规划
62. Unique Paths
QuestionEditorial Solution
Total Accepted: 86710 Total Submissions: 239084 Difficulty: Medium
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?
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAaoAAADICAIAAAA2pjJEAAAAA3NCSVQICAjb4U/gAAAgAElEQVR4Xu2dCXwURfbHq7vnvnPfIQQSwhnuU2VFVFgQvBXXG/EGvHVX99L9q+u6qKyK4vEH/yK6CAreoCDIFe47EAK578zkmHt6uvv/OoORKyTpzJAe5pXz4TP2VFW/+lb3L6+6ql5TgiAQTEgACSCByCNAR16TscVIAAkgAZEAyh9eB0gACUQoAZS/CO14bDYSQAIof3gNIAEkEKEEUP4itOOx2UgACYRY/vxs2YaN7obGDoL2WK2s3d7BzJgNCSABJNAVAqGVv03/fv2L8Zev++Ozfre7XSsrtu/4bMr0VQ/NdtfXt5sZMyABJIAEukggtPJXfmA/SzzlnyxnPd52Da3YubMmb1Pl/y2u2Laj3cyYAQkgASTQRQKhlb/k3tkge3FTJyo16nYN1cXGUoQo0jIN8fHtZsYMSAAJIIEuEqBCvetjy/Qbcub9M6pXZvuGCsKuF/9Jm0yDZz/YfmbMgQSQABLoGgFF14q3XzqqZw9jTHT7+SAHRaUNHcI5HB3KjJmQABJAAl0jENrBL9gm8J3YUyzwncndtZZjaSSABCKcQJC9P8Hng9E0PMIDX04ggsCyvM/rs9nEuAoC/xtriuLc7tPG3bRC4bNa4biropLzelsqERNk08THK42G34rjNySABJBAlwkE89mfq7SsbMknoIAwjBVly8/5Kiqd+w5oMjMoxSk6S9G0p/CY4GUDOU+0gqJ41kd43m9t4KqqCWFAQ+HDk+aM1+b3fmR2lxuLFSABJIAEfiMQTPmr/HxFwQOPEJoSfH5QMTgJpdPAR5Q5njuJOgV5KLVKPELRhFGIjiFkh4E4xRBO1ESKocW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CKRAMdx48aNu/vuu1sbX1cnvu0InDuNRgO+IXz3+/2gffAFXMLo6Oh169bBaPfVV18dPHjwvHnz1Gp1QC4hW0gJ4uA3pHixciQQcQRA5sDvO63ZoIlwBDw+0Dv4Av8L2SCB97dt27arrrpq4sSJiz9avHP7bo/HG/gVsoH8QZ7QEUTvL3RssWYkEIkEzGYzjF5PbjksgsnKygJ379JLL4WZDfgpIyNDp9OBiwcj3BEjRsyaee+jjz4KM8Vvv/1WTEx0//79YUQM2WCwDL5h6CBSEsQ1v5JkJRBFh3cyh876c9QMRioVndhufY6qQvcTuP9VDWRUr9CdIWg1l9vE8LwQfy2Ef4u7bCyMpiBInEFNUkN4y3TZypYK7B5SWENijDAkDE6FIaqF5QjrJ307E3ce/Dvw8gL6FbAKjoA/CCNfmAaBgS2MfyED+H2Q4Asch2xOpxP+N/AdpkSgeOAZIhwMDITP0UBgWFRPEs2djg8o0ftrGbbLPYGNop0yvrzkbd3p/Sv2OUUoGfMUzYPP6YbL8f8Dd5DcTZUEE9TtZO0D+nAkoGutx0EEA70SOA7f9frfAryAYxj4tTXbubtQcqdLkj+B1NmJQsaPDQGH+FeLIwq/rP+6gp0MRerPxwLPc18/7fwKdnr94ocT5M4TQsUJPLE6ZG0nKLTbK0a1Ax9Qzt4f9LufIzQld568QDyS5kikDH4bnOIQQ+5/uNq5o/FnJNDNBMCNxpsoKH0AJCGqYLSh0z6ZFPkLisVYCRJAAkigewnIeATbvWDw7EgACVzoBFD+LvQexvYhASTQBgGUvzbA4GEkgAQudAIofxd6D2P7kAASaIMAyl8bYPAwEkACFzoBlL8LvYexfUgACbRBAOWvDTB4GAkggQudAMrfhd7D2D4kgATaIIDy1wYYPIwEkMCFTgDl70LvYWwfEkACbRBA+WsDDB5GAkjgQieA8neh9zC2DwkggTYIoPy1AQYPIwEkcKET+H+pRgMmwQIxVwAAAABJRU5ErkJggg==" alt="" />
使用动态规划来求解本题,在每个节点处,只有两个选择,向下或者向右走。本节点可由上一个节点向下或者向右走过来,两种方法:
1)使用一个二位数组count[i][j]来表示从起始点到达坐标(i,j)的,有两种,(i-1,j)-->(i,j)及(i,j-1)-->(i,j),即count[i][j] = count[i-1][j]+count[i][j-1],此时时间复杂度为O(mn),空间复杂度为O(mn)
public class Solution {
public int uniquePaths(int m, int n) {
int[][] count = new int[m+1][n+1];
for(int i=0;i<=m;i++){
for(int j=0;j<=n;j++){
if(i==0||j==0){
count[i][j] = 0;
}else if(i==1&&j==1){
count[i][j]=1;
}else{
count[i][j] = count[i-1][j]+count[i][j-1];
}
}
}
return count[m][n];
}
}
2)使用一个数组count[i]来表示从起始点到达某一行某一列的路径数量,此时有,count[i] = count[i-1]+count[i],此时,时间复杂度为O(mn),空间复杂度为O(n):
//因为只需求得最终结果,而不需要知道到达每一个坐标的路径数量
public class Solution {
public int uniquePaths(int m, int n) {
int[] count = new int[n];
count[0]=1;
for(int i=0;i<m;i++){
for(int j=1;j<n;j++){
count[i] = count[i-1]+count[i];
}
}
return count[n-1];
}
}
63. Unique Paths II
QuestionEditorial Solution
Follow up for "Unique Paths":
Now consider if some obstacles are added to the grids. How many unique paths would there be?
An obstacle and empty space is marked as 1
and 0
respectively in the grid.
For example,
There is one obstacle in the middle of a 3x3 grid as illustrated below.
[
[0,0,0],
[0,1,0],
[0,0,0]
]
The total number of unique paths is 2
.
LeetCode之动态规划的更多相关文章
- leetcode笔记 动态规划在字符串匹配中的应用
目录 leetcode笔记 动态规划在字符串匹配中的应用 0 参考文献 1. [10. Regular Expression Matching] 1.1 题目 1.2 思路 && 解题 ...
- Leetcode之动态规划(DP)专题-详解983. 最低票价(Minimum Cost For Tickets)
Leetcode之动态规划(DP)专题-983. 最低票价(Minimum Cost For Tickets) 在一个火车旅行很受欢迎的国度,你提前一年计划了一些火车旅行.在接下来的一年里,你要旅行的 ...
- Leetcode之动态规划(DP)专题-647. 回文子串(Palindromic Substrings)
Leetcode之动态规划(DP)专题-647. 回文子串(Palindromic Substrings) 给定一个字符串,你的任务是计算这个字符串中有多少个回文子串. 具有不同开始位置或结束位置的子 ...
- Leetcode之动态规划(DP)专题-474. 一和零(Ones and Zeroes)
Leetcode之动态规划(DP)专题-474. 一和零(Ones and Zeroes) 在计算机界中,我们总是追求用有限的资源获取最大的收益. 现在,假设你分别支配着 m 个 0 和 n 个 1. ...
- Leetcode之动态规划(DP)专题-486. 预测赢家(Predict the Winner)
Leetcode之动态规划(DP)专题-486. 预测赢家(Predict the Winner) 给定一个表示分数的非负整数数组. 玩家1从数组任意一端拿取一个分数,随后玩家2继续从剩余数组任意一端 ...
- Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II)
Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II) 编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n ...
- Leetcode之动态规划(DP)专题-198. 打家劫舍(House Robber)
Leetcode之动态规划(DP)专题-198. 打家劫舍(House Robber) 你是一个专业的小偷,计划偷窃沿街的房屋.每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互 ...
- Leetcode之动态规划(DP)专题-121. 买卖股票的最佳时机(Best Time to Buy and Sell Stock)
Leetcode之动态规划(DP)专题-121. 买卖股票的最佳时机(Best Time to Buy and Sell Stock) 股票问题: 121. 买卖股票的最佳时机 122. 买卖股票的最 ...
- Leetcode之动态规划(DP)专题-122. 买卖股票的最佳时机 II(Best Time to Buy and Sell Stock II)
Leetcode之动态规划(DP)专题-122. 买卖股票的最佳时机 II(Best Time to Buy and Sell Stock II) 股票问题: 121. 买卖股票的最佳时机 122. ...
- Leetcode之动态规划(DP)专题-123. 买卖股票的最佳时机 III(Best Time to Buy and Sell Stock III)
Leetcode之动态规划(DP)专题-123. 买卖股票的最佳时机 III(Best Time to Buy and Sell Stock III) 股票问题: 121. 买卖股票的最佳时机 122 ...
随机推荐
- RSA 每次公钥加密不同结果
今天服务器端一哥们突然跑过来跟我说:我发现公钥每次加密都不同结果啊? 我说:怎么可能?不同的话,私要怎么解密和验证啊? 然后我屁颠屁颠的试了下,结果发现不论在在线RSA的还是自己公司 利用同一个明文加 ...
- Python3 shutil模块
平时我们总会用到复制文件的命令,Python中自带了相应模块,那就是shutil模块,下面是shutil模块的分析及用法. 1.copyfileobj(fsrc, fdst, length=16*10 ...
- Http中Get/Post请求区别
Http中Get/Post请求区别 (1)get是从服务器上获取数据,post是向服务器传送数据. (1) 在客户端,Get方式在通过URL提交数据,数据在URL中可以看到:POST方式,数据放置 ...
- vb编写串口调试程序
sub是子模块,可以调用但是没有返回值,function是有返回值的. public sub 可以在其它form里调用,而private sub 只能在当前form里调用. vb里的if else , ...
- BW建模开发入门
本文档主要指导具体操作步骤,一些技术名称和描述可能在各步骤中不对应,可以忽略 一.模型建立 1.建立信息区和信息对象目录 1)进入BW工作台 2)创建信息区 输入技术名称和描述 3)创建特性和关键值的 ...
- Linux环境下Maven的.m2文件夹
aven中的.m2文件夹 安装完maven是没有.m2文件夹的.在linux中以.开头的文件夹都是隐藏的.当使用maven命令的时候,maven自动会创建.m2文件夹. 运行命令mvn help:sy ...
- 11.Python使用Scrapy爬虫小Demo(新手入门)
1.前提:已安装好scrapy,且已新建好项目,编写小Demo去获取美剧天堂的电影标题名 2.在项目中创建一个python文件 3.代码如下所示: import scrapy class movies ...
- 抽象工厂模式(abstract)创建型模式
(一)简单工厂模式? 现在的学习是面向对象面向接口的,但是执行时的操作需要实例化后的对象.随着我们需要的类的增加,我们就需要把这些共同的东西提取出来,放在一个抽象类中,让这些子类来继承抽象类.当我们调 ...
- erlang入门之编译和运行
测试erlang脚本如下 -module(empty). -author("mmc"). %% API -export([test/1,test/0]). test()-> ...
- 【转】如何使用JMeter测试Java项目
一. Apache JMeter工具 1)简介 JMeter——一个100%的纯Java桌面应用,它是 Apache组织的开放源代码项目,它是功能和性能测试的工具.JMeter可以用于测试静态或者动态 ...