leetcode-62. Unique Paths · DP + vector
题面
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?
Note: m and n will be at most 100.

说白了就是:统计从二维数组左上角到右下角总共有多少不同路径。(0 <= m, n <= 100)
样例
Example 1:
Input: m = 3, n = 2
Output: 3
Explanation:
From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
1. Right -> Right -> Down
2. Right -> Down -> Right
3. Down -> Right -> RightExample 2:
Input: m = 7, n = 3
Output: 28
思路
由于只能朝下或者右走,稍加推导,我们就可以看出:当前点的路径数就等于它左边点路径数加上上边点路径数,很容易想到递归(很不幸,层数过大,栈会溢出!)。so, 我们只能通过DP循环来做。
算法 : DP
时间复杂度:O(m*n)
空间复杂度:O(m*n)
1. 用二维数组还是一维数组记录状态都可以,我们先用二维来说明问题。即:创建二维数组dp[m][n]
2. 预处理第一行和第一列,因为第一行只能往右走,第一列只能往下走(只有一条路径,所以都初始化为1)
3. 遍历二维DP数组:当前点路径=上边点路径+左边点路径
状态方程:
dp[0][j] = 1
dp[i][0] = 1
dp[i][j] = dp[i-1][j] + dp[i][j-1]
源码
int uniquePaths(int m, int n) {
if(m == || n == )
return ;
int dp[n][m] = {};
dp[][] = ;
for(int i=; i<m; i++)
dp[][i] = ;
for(int i=; i<n; i++)
dp[i][] = ;
for(int i=; i<n; i++)
{
for(int j=; j<m; j++)
{
dp[i][j] = dp[i][j-] + dp[i-][j];
}
}
return dp[n-][m-];
}
优化:空间优化
上面算法,我们使用了二维数组记录DP状态,其实用一维就够了。推到一个简单的例子你就会发现,焦点总是在一行上,只要用一行从上到下滑动,就可达到目的。
时间复杂度:O(m*n)
空间复杂度:O(n)
源码
int uniquePaths(int m, int n) {
//空间压缩
if(m == || n == )
return ;
int dp[m] = {};
for(int i=; i<m; i++)
dp[i] = ;
for(int i=; i<n; i++)
{
dp[] = ;
for(int j=; j<m; j++)
{
dp[j] = dp[j-] + dp[j];
}
}
return dp[m-];
}
leetcode-62. Unique Paths · DP + vector的更多相关文章
- leetcode 62. Unique Paths 、63. Unique Paths II
62. Unique Paths class Solution { public: int uniquePaths(int m, int n) { || n <= ) ; vector<v ...
- [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 ...
- [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 ...
- [leetcode] 62 Unique Paths (Medium)
原题链接 字母题 : unique paths Ⅱ 思路: dp[i][j]保存走到第i,j格共有几种走法. 因为只能走→或者↓,所以边界条件dp[0][j]+=dp[0][j-1] 同时容易得出递推 ...
- 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 ...
- [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 ...
- LeetCode 62. Unique Paths不同路径 (C++/Java)
题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...
- LeetCode: 62. Unique Paths(Medium)
1. 原题链接 https://leetcode.com/problems/unique-paths/description/ 2. 题目要求 给定一个m*n的棋盘,从左上角的格子开始移动,每次只能向 ...
- LeetCode 63. Unique Paths II不同路径 II (C++/Java)
题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...
随机推荐
- Greenwich.SR2版本的Spring Cloud Eureka实例
作为微服务架构中最为核心和基础的服务治理,注册中心提供了微服务实例的自动化注册与发现.而作为一个服务注册中心,eureka的作用与传统的zk.etcd的作用是一样的,同样也支持高可用(集群).不同之处 ...
- 【微信小程序】wx.navigateBack() 携带参数返回
第一个页面: go_pick_time:function(e){ var that = this; var type = e.currentTarget.dataset.type; wx.naviga ...
- 怎么用Ubuntu系统制作Ubuntu系统盘
ubuntu 16 下载地址: http://releases.ubuntu.com/16.04/?_ga=2.211639766.273896083.1564759714-49163328.1564 ...
- MYSQL 启动问题
1.日志中出现 [ERROR] InnoDB: The Auto-extending innodb_system data file './ibdata1' is of a different si ...
- C# WebApi 接口传参详解
这篇文章主要介绍了C# WebApi 接口传参详解,本篇打算通过get.post.put.delete四种请求方式分别谈谈基础类型(包括int/string/datetime等).实体.数组等类型的参 ...
- python reduce和偏函数partial
functools模块 reduce方法: reduce方法 reduce方法,顾名思义就是减少 可迭代对象不能为空,初始值没提供就在可迭代对象中去一个元素 from functools import ...
- wordpress通过$wpdb获取一个分类下所有的文章
在wordpress程序根目录下新建一个php文件,粘贴下面的代码 如下面的代码注释,修改$CID这个分类id,就可以获取这个分类下的文章了.这个查询需要联合三个表wp_posts.wp_term_r ...
- MMC.EXE应用程序错误 应用程序无法正常启动(0XC0000043)
一.Windows+R 输入 regedit.exe ①打开注册表: HKEY_LOCAL_MACHINE“"SOFTWARE""Classes""C ...
- 【计算机视觉】OPENCV对于有alpha通道的透明背景图片的读取和图片叠加
这个是我自己做的粗略的螺旋丸的图,导出为png并带有alpha通道. 最后和一只狗合成成这个样子. 效果还是可以的. 为了实现这个效果,首先我们要明白具有透明通道的图片的OpenCV的读取方式.在Op ...
- 【数据库开发】Redis key-value内存数据库介绍
Redis是一个开源的,先进的 key-value 存储可用于构建高性能,可扩展的 Web 应用程序的解决方案.Redis官方网网站是:http://www.redis.io/,如下: Redis 有 ...