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 ...
随机推荐
- Linux--DNS服务器
DNS是Internet上使用最普遍,也是最重要的服务之一,通过DNS我们才可以访 问丰富多彩的网络,而DNS服务器就是为了实现域名解析功能而搭建的. 域名系统采用层次结构,按地理区域或机构区域 ...
- javascript之页面打印
WebBrowser组件是IE内置的浏览器控件,使用时,首先要在<body>标签的下面用<object>...</object>标记声明WebBrowser组件,代 ...
- Android View架构总结
View和Activity的区别 android的四大组件,Activity是四大组件中唯一一个用来和用户进行交互的组件.可以说Activity就是android的视图层. 如果再细化,Activit ...
- android最新更新方法
使用SDK Manager更新时出现问题Failed to fetch URL https://dl-ssl.google.com/android/repository/repository-6.xm ...
- Java中怎么简单的使用正则表达式?
对于正则表达式,我通常的认识就是通过一些陌生的奇怪的符号就可以完成很复杂事件的好帮手!实际上正则表达式确实是这方面的好助手,接下来让我们一起认识一下Java中怎么使用正则表达式吧. 初见Pattern ...
- ios第三方数据请求 UI_15
AppDelegate.m //指定根视图 self.window.rootViewController = [[[UINavigationController alloc]initWithRootV ...
- 在maven中开发Spring需要的jar依赖
在maven中开发Spring需要的jar依赖 <properties> <spring.version>4.0.6.RELEASE</spring.version> ...
- UNIX环境高级编程——文件和目录
一.获取文件/目录的属性信息 int stat(const char *path, struct stat *buf); int fstat(int fd, struct stat *buf); in ...
- Socket编程实践(9) --套接字IO超时设置方法
引:超时设置3种方案 1. alarm超时设置方法 //代码实现: 这种方式较少用 void sigHandlerForSigAlrm(int signo) { return ; } signal(S ...
- 【UI 设计】PhotoShop基础工具 -- 移动工具
还是学点美工的东西吧, 业余爱好 比学编程还难 PS版本 : PhotoShop CS6 1. 移动工具 (1) 工具栏和属性栏 工具栏 和 属性栏 : 左侧的是工具栏, 每选中一个工具, 在菜单 ...