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). ...
随机推荐
- c#子线程执行完怎么通知主线程(转)
定义一个委托实现回调函数 public delegate void CallBackDelegate(string message); 程序开始的时候 //把回调的方法给委托变量 CallBackDe ...
- python数据分析数据标准化及离散化详解
python数据分析数据标准化及离散化详解 本文为大家分享了python数据分析数据标准化及离散化的具体内容,供大家参考,具体内容如下 标准化 1.离差标准化 是对原始数据的线性变换,使结果映射到[0 ...
- 【设计思路】Booking优化
https://www.uisdc.com/booking-redesign https://medium.muz.li/booking-com-ux-case-study-7ffb39e54791
- JEECG新建用户不用系统用户表的实现
首先新增组织机构 和 角色: -- 新增 组织机构 INSERT INTO `t_s_depart` VALUES ('dept001', '你所在的机构', '你所在的机构的描述', null, ' ...
- css调用字体 没装微软雅黑,用css写@font-face让其能显示微软雅黑字体
在设计布局网页时 经常想要用一些比较好看的字体,比如微软雅黑,这个字体在近年来在网页设计中运用越来越平常, 然而所使用的字体也只有自己能看到 到别的机子上 又恢复了原来的宋体神马的. 经过一位高手的提 ...
- 解决访问github等网站慢或下载失败的问题
最近老大push项目,正常的git clone每次都是下载一部分就断掉了.下面介绍网上找到的两种方法: 方法一: 1.打开网站https://www.ipaddress.com/: 2.分别在上面打开 ...
- 白嫖百度 Tesla V100 笔记(在 AI Studio 上使用 tensorflow 和 pytorch 的方法)
登陆百度 AI Studio 并按照教程创建新项目 启动项目并进入控制台 下载 Anaconda3/Miniconda3 安装脚本 安装在 ~/work/*conda3 目录 输入命令 source ...
- vue时间戳转换(10位数)/(13位)
<template> <!-- time为时间戳 --> <div>{{time | formatDate}}</div> <!-- 结果为 20 ...
- 【计算机视觉】OPENCV对于有alpha通道的透明背景图片的读取和图片叠加
这个是我自己做的粗略的螺旋丸的图,导出为png并带有alpha通道. 最后和一只狗合成成这个样子. 效果还是可以的. 为了实现这个效果,首先我们要明白具有透明通道的图片的OpenCV的读取方式.在Op ...
- 记录项目中easyui的运用
1.实现合并列,且文字显示居左,背景为固定颜色 效果图: 实现代码: $('#tab').datagrid({ title : '', //表格标题 iconCls : 'icon-list', // ...