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问题,给定一个m*n的二维数组,每一个值都是非负数。问从起点(左上角)到终点(右下角)的一条路径上所有的数加起来的和最小是多少?

解题思路:

参考  Unique Path Two题目的解法  在那个基础上:

以中间的某一个点A来考虑,A左边是B,A上边是C,假设B和C的结果已经求出来了,那么A的结果应该等于B和C中结果较小的那个加上A位置的给定值。

特殊的是:

第一行中,除第一个点的所有的点的结果等于前一个点的结果加上本身的给定值。

第一列中,除第一个点的所有的点的结果等于上一个点的结果加上本身的给定值。

(描述起来好绕口的说…………)

画图来理解:

代码如下:

空间复杂度:O(n)

class Solution {
public:
int minPathSum(vector<vector<int> > &grid) {
int m = grid.size();
int n = grid[].size();
vector<int> dp(n,);
for(int i = ; i < m;i++){
for(int j = ; j < n; j++){
if(j == ){
dp[j] = dp[j] + grid[i][j];
}
else if(i == ){
dp[j] = dp[j-] + grid[i][j];
}
else{
dp[j] = min(dp[j-],dp[j]) + grid[i][j];
}
}
}
return dp.back();
}
};

上面的比较容易理解,下面的代码其实是一个道理:

class Solution {
public:
int minPathSum(vector<vector<int> > &grid) {
int m = grid.size();
int n = grid[].size();
vector<int> dp(n+,INT_MAX);
dp[] = ;
for(int i = ; i < m; i++){
for(int j = ; j < n; j++){
dp[j+] = min(dp[j],dp[j+]) + grid[i][j];
}
}
return dp.back();
}
};

【LeetCode练习题】Minimum Path Sum的更多相关文章

  1. [Leetcode Week9]Minimum Path Sum

    Minimum Path Sum 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/minimum-path-sum/description/ Descr ...

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

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

  4. [LeetCode] 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 ...

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

  6. 【leetcode】Minimum Path Sum(easy)

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  7. Java for 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 which ...

  8. 【题解】【矩阵】【DP】【Leetcode】Minimum Path Sum

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  9. C#解leetcode 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 ...

  10. leetcode:Minimum Path Sum(路线上元素和的最小值)【面试算法题】

    题目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right w ...

随机推荐

  1. Socket通信原理和实践

    我们深谙信息交流的价值,那网络中进程之间如何通信,如我们每天打开浏览器浏览网页时,浏览器的进程怎么与web服务器通信的?当你用QQ聊天时,QQ进程怎么与服务器或你好友所在的QQ进程通信?这些都得靠so ...

  2. What a Mess(二分)

    What a Mess Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit  ...

  3. MFC读写配置文件

    void CFileTextDoc::OnIniread() { // TODO: Add your command handler code here CString strStudName;   ...

  4. Table表格的一些操作

    首先创建一个table表格: <input type="button" id="btn1" value="获取数据" /> &l ...

  5. 怎么使用jQuery在DIV适应屏幕大小一直居中

    js的代码是这样的: $(function(){ $(window).resize(function(){ $(".login").css({ position: "ab ...

  6. Python初学

    经同学推荐,学习了下Python语言,看Python的介绍,它本身是一个面向对象的解释型脚本语言,我初看到这句话的时候就在想,一个脚本语言还搞成面向对象?有这个必要么?原谅我肤浅了一把. 它还被俗称为 ...

  7. Citrix 服务器虚拟化之十二 Xenserver灾难恢复

    Citrix 服务器虚拟化之十二 Xenserver灾难恢复 (环境有限实验无法测试,配置步骤摘取自官方文档) XenServer 灾难恢复的工作原理在存储库(SR)上还原从主(生产)环境复制到备份环 ...

  8. spring3+hibernate3+(dbcp+oracle+拦截器事务配置)整合(一)

    1.applicationContext-base.xml文件 <?xml version="1.0" encoding="UTF-8"?>< ...

  9. [原]C语言实现的快速排序,采用分治策略,递归实现

    #include<stdio.h> #define LEN 8 int a[LEN] = { 5, 2, 4, 7, 1, 3, 2, 6 }; int Partition(int a[] ...

  10. 利用cookies获取登录后的网页

    众所周知,HTTP连接是无状态的,那么问题来了,怎么记录用户的登录信息呢?通常的做法是用户第一次发送HTTP请求时,在HTTP Server端生成一个SessionID,SessionID会对应每个会 ...