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. JQuery 补充

    筛选: expr         String 字符串值,包含供匹配当前元素集合的选择器表达式. jQuery      objectobject 现有的jQuery对象,以匹配当前的元素. elem ...

  2. pyqt 同时勾选多个items(网友提供学习)

    框选多个item之后,用空格键可以勾选/去选多个item,效果如下图所示: http://oglop.gitbooks.io/pyqt-pyside-cookbook/list/img/checkbo ...

  3. Leetcode 238 Product of Array Except Self 时间O(n)和空间O(1)解法

    1. 问题描写叙述 给定一个n个整数的数组(n>1n>1)nums,返回一个数组output,当中的元素outputioutput_i的值为原数组nums中除numsinums_i之外的全 ...

  4. [Git] --no-verify

    Somtimes, the project might set the commit message guide line, if your commit doesn't meet the requi ...

  5. 从零开始学C++之STL(七):剩下5种算法代码分析与使用示例(remove 、rotate 、sort、lower_bound、accumulate)

    一.移除性算法 (remove)  C++ Code  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 ...

  6. openssl ans.1编码规则分析及证书密钥编码方式

    1 数据编码格式 openssl的数据编码规则是基于ans.1的,ans.1是什么 ? 先上高大上的解释 ASN.1(Abstract Syntax Notation One), 是一种结构化的描述语 ...

  7. CSS基础知识笔记(四)

    元素分类 标签元素大体被分为三种不同的类型:块状元素.内联元素(又叫行内元素)和内联块状元素. 常用的块状元素有: <div>.<p>.<h1>...<h6& ...

  8. C# 仿百度自动匹配

    private void Form1_Load(object sender, EventArgs e) { AutoCompleteStringCollection source = new Auto ...

  9. Oracle CASE WHEN 用法介绍[Z]

    Oracle CASE WHEN 用法介绍 1. CASE WHEN 表达式有两种形式 --简单Case函数 CASE sex WHEN '1' THEN '男' WHEN '2' THEN '女' ...

  10. setNeedsDisplay、layoutSubViews

    UIView的setNeedsDisplay和setNeedsLayout方法.首先两个方法都是异步执行的.而setNeedsDisplay会调 用自动调用drawRect方法,这样可以拿到UIGra ...