LeetCode之“动态规划”:Minimum Path Sum && Unique Paths && Unique Paths II
之所以将这三道题放在一起,是因为这三道题非常类似。
1. Minimum Path Sum
题目要求:
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.
Note: You can only move either down or right at any point in time.
该题解答参考自一博文。
设dp[i][j]表示从左上角到grid[i][j]的最小路径和。那么dp[i][j] = grid[i][j] + min( dp[i-1][j], dp[i][j-1] );
下面的代码中,为了处理计算第一行和第一列的边界条件,我们令dp[i][j]表示从左上角到grid[i-1][j-1]的最小路径和,最后dp[rows][cols]是我们所求的结果
int minPathSum(vector<vector<int>>& grid) {
int rows = grid.size();
if(rows == )
return ;
int cols = grid[].size();
vector<vector<int> > dp(rows + , vector<int>(cols + , INT_MAX));
dp[][] = ;
for(int i = ; i < rows + ; i++)
for(int j = ; j < cols + ; j++)
dp[i][j] = grid[i - ][j - ] + min(dp[i][j - ], dp[i - ][j]);
return dp[rows][cols];
}
注意到上面的代码中dp[i][j] 只和上一行的dp[i-1][j]和上一列的dp[i][j-1]有关,因此可以优化空间为O(n)(准确来讲空间复杂度可以是O(min(row,col)))
int minPathSum(vector<vector<int>>& grid) {
int rows = grid.size();
if(rows == )
return ;
int cols = grid[].size();
vector<int> dp(cols + , INT_MAX);
dp[] = ;
for(int i = ; i < rows + ; i++)
for(int j = ; j < cols + ; j++)
dp[j] = grid[i-][j-] + min(dp[j-], dp[j]);
return dp[cols];
}
2. 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.
这道题的解答跟上一道题是非常类似的,程序如下:
int uniquePaths(int m, int n) {
if(m == && n == )
return ;
vector<vector<int> > dp(m, vector<int>(n, ));
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-];
}
优化空间程序:
int uniquePaths(int m, int n) {
if(m == && n == )
return ;
vector<int> dp(n, );
for(int i = ; i < m; i++)
for(int j = ; j < n; j++)
dp[j] = dp[j-] + dp[j];
return dp[n - ];
}
3. Unique Paths II
题目要求:
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.
[
[,,],
[,,],
[,,]
]
The total number of unique paths is 2.
Note: m and n will be at most 100.
这道题跟上一道题基本一致,不同的地方在于我们需要将能到达存在obstacle的地方的路径数置为0。程序如下:
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
int rows = obstacleGrid.size();
if(rows == )
return ;
int cols = obstacleGrid[].size();
if(cols == )
return ;
vector<vector<int> > dp(rows, vector<int>(cols, ));
int i = ;
while(i < rows)
{
if(obstacleGrid[i][] == )
while(i < rows)
{
dp[i][] = ;
i++;
}
i++;
}
int j = ;
while(j < cols)
{
if(obstacleGrid[][j] == )
while(j < cols)
{
dp[][j] = ;
j++;
}
j++;
}
for(i = ; i < rows; i++)
for(j = ; j < cols; j++)
{
if(obstacleGrid[i][j] == )
dp[i][j] = ;
else
dp[i][j] = dp[i-][j] + dp[i][j-];
}
return dp[rows-][cols-];
}
LeetCode之“动态规划”:Minimum Path Sum && Unique Paths && Unique Paths II的更多相关文章
- 【LeetCode练习题】Minimum Path Sum
Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...
- 【LeetCode】64. Minimum Path Sum
Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...
- 【一天一道LeetCode】#64. Minimum Path Sum.md
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】64. Minimum Path Sum 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LeetCode OJ 64. Minimum Path Sum
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- leetcode || 64、Minimum Path Sum
problem: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom ri ...
- LeetCode OJ:Minimum Path Sum(最小路径和)
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- 【LeetCode】064. Minimum Path Sum
题目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right w ...
- [leetcode DP]64. Minimum Path Sum
一个m*n的表格,每个格子有一个非负数,求从左上到右下最短的路径值 和62,63两个值是同一个思路,建立dp表,记录每个位置到右下角的最短路径的值 class Solution(object): de ...
- [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 ...
随机推荐
- Activity平移动画
Activity平移动画 效果图 添加动画文件 在res下添加anim文件夹,在anim下添加几个动画文件,分别是进入和退出的动画时间和移动距离,属性很简单,一看就懂,不磨叽了. tran_next_ ...
- JAVA面向对象-----多态
多态的概述 1:什么是多态 一个对象的多种状态 (老师)(员工)(儿子) 教师 a =老钟; 员工 b= 老钟; 2:多态体现 1:Father类 1:非静态成员变量x 2:静态成员变量y 3:非静态 ...
- SSH网上商城---使用ajax完成用户名是否存在异步校验
小伙伴在上网的时候,需要下载或者观看某些视频资料,更或者是在逛淘宝的时候,我们都需要注册一个用户,当我们填写好各种信息,点击确定的时候,提示用户名已经存在,小编就想,为什么当我们填写完用户名的时候,她 ...
- 【iOS 开发】基本 UI 控件详解 (UIButton | UITextField | UITextView | UISwitch)
博客地址 : http://blog.csdn.net/shulianghan/article/details/50051499 ; 一. UI 控件简介 1. UI 控件分类 UI 控件分类 : 活 ...
- Quick-Cocos2d-X 捋一捋框架流程
猴子原创,欢迎转载.转载请注明: 转载自Cocos2D开发网–Cocos2Dev.com,谢谢! 原文地址: http://www.cocos2dev.com/?p=535 一直比较关注Quick L ...
- open_links_per_instance 和 open_links 参数说明
1.1 OPEN_LINKS Property Description Parameter type Integer Default value 4 Modifiable No --即修改需要重启实 ...
- 套接字输入缓冲装置——InternalInputBuffer
互联网的世界很复杂,信息从一端传向另一端过程也相当复杂,中间可能通过若干个硬件,为了提高发送和接收效率,在发送端及接收端都将引入缓冲区,所以两端的套接字都拥有各自的缓冲区,当然这种缓冲区的引入也带来了 ...
- 如何判断webview是不是滑到底部
getScrollY()方法返回的是当前可见区域的顶端距整个页面顶端的距离,也就是当前内容滚动的距离. getHeight()或者getBottom()方法都返回当前webview这个容器的高度 ge ...
- 如何在Cocos2D游戏中实现A*寻路算法(二)
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...
- Rational Rose正逆向工程(类图转Java代码,Java代码转类图)
一,正向工程 1.设置默认语言为Java,Tools->Options->Notation->default:选择Java. 2.设置环境变量Class ...