之所以将这三道题放在一起,是因为这三道题非常类似。

  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的更多相关文章

  1. 【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 ...

  2. 【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 ...

  3. 【一天一道LeetCode】#64. Minimum Path Sum.md

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  4. 【LeetCode】64. Minimum Path Sum 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 【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 ...

  9. [leetcode DP]64. Minimum Path Sum

    一个m*n的表格,每个格子有一个非负数,求从左上到右下最短的路径值 和62,63两个值是同一个思路,建立dp表,记录每个位置到右下角的最短路径的值 class Solution(object): de ...

  10. [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 ...

随机推荐

  1. SQL Server 扩展事件(Extented Events)从入门到进阶(3)——通过界面操作Extented Event

    本文属于 SQL Server扩展事件(Extended Events)从入门到进阶 系列 对于接纳扩展事件,其中一个最大的障碍就是要对XML和XQuery有一定的了解以便分析数据.我们可以使用T-S ...

  2. 20 ViewPager Demo3指示器

    MainActivity.java package com.qf.day20_viewpager_demo3; import java.util.ArrayList; import java.util ...

  3. TCP连接建立系列 — 客户端接收SYNACK和发送ACK

    主要内容:客户端接收SYNACK.发送ACK,完成连接的建立. 内核版本:3.15.2 我的博客:http://blog.csdn.net/zhangskd 接收入口 tcp_v4_rcv |--&g ...

  4. 编译GDAL支持MySQL

    GDAL支持MySQL需要MySQL的库才可以,编译很简单,修改nmake.opt文件中对应的MySQL的库的路径和lib即可. nmake.opt文件中397行左右,如下: # MySQL Libr ...

  5. 基于V4L2摄像头采集图片程序设计

    #ifndef __COMMON_H #define __COMMON_H //该头文件定义的是摄像头在屏幕上显示的宽度和高度 #include<stdio.h> #include< ...

  6. 为你的MacOS App添加开机自启动(Swift)

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/52104828 ...

  7. OpenCV特征点检测匹配图像-----添加包围盒

    最终效果: 其实这个小功能非常有用,甚至加上只有给人感觉好像人脸检测,目标检测直接成了demo了,主要代码如下: // localize the object std::vector<Point ...

  8. Android初级教程获取手机系统联系人信息

    在手机内部,对联系人信息存在对应的数据库.我们创建的而联系人信息都存在这张表中.如下是对数据库的截图,我已经对表和应该注意的地方做了红笔标注: 好了,现在可以根据数据库里面的数据来写代码了. 代码如下 ...

  9. Hessian探究(一)Hessian与springMVC结合

    上一篇博客Hessian探究(一)Hessian入门示例我们初步简单的介绍了一下Hessian的使用入门示例,我们是通过Servlet来暴露Hessian的对外服务的,接下来我们介绍一下通过Sprin ...

  10. UNIX环境高级编程——Linux系统调用列表

    以下是Linux系统调用的一个列表,包含了大部分常用系统调用和由系统调用派生出的的函数.这可能是你在互联网上所能看到的唯一一篇中文注释的Linux系统调用列表,即使是简单的字母序英文列表,能做到这么完 ...