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. Python Cookie HTTP获取cookie并处理

    Cookie模块同样是Python标准库中的一员,它定义了一些类来解析和创建HTTP 的 cookie头部信息. 一.创建和设置Cookie >>> import Cookie #导 ...

  2. Android应用开发学习之状态栏通知

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 状态栏通知涉及到两个类,一是Notification,它代表一个通知:另一个是NotificationManager ...

  3. JAVA获得系统配置文件的System Properties

    来个java获得系统配置文件的 public class SystemProperties { public static void main(String[] args) { Properties ...

  4. UIView 转 UIImage

    这个方法很实用,特别是在做水印相机得时候... - (UIImage*) imageWithUIView:(UIView*) view{ // 创建一个bitmap的context // 并把它设置成 ...

  5. IOS 采用https 协议访问接口

    申请好证书后,发现ios 仍无法使用https协议访问到数据,发现ios 需要ssl 支持 TLS1.2 . 更改nginx 配置: ssl_protocols TLSv1 TLSv1. TLSv1. ...

  6. 原生AJAX如何异步提交数据?

    AJAX概述 AJAX:Asynchronous Javascript And XML,异步的JS和XML.2001,Google为了改进搜索的用户体验,提出了GoogleSugguest效果,正式提 ...

  7. Jquery面试题整合

    来自棱镜学院-在线IT教育     www.prismcollege.com 一.Jquery測试题 以下哪种不是jquery的选择器?(单选) A.基本选择器 B.后代选择器 C.类选择器 D.进一 ...

  8. IOS基于新浪微博开放平台微博APP

    1.基于新浪微博开放平台APP源码 2.gitHub源代码下载地址 https://github.com/whzhaochao/SinaWeiBoOpen 3.用到的第三放开源库 3.1  RTLab ...

  9. Android应用程序注冊广播接收器(registerReceiver)的过程分析

    前面我们介绍了Android系统的广播机制,从本质来说,它是一种消息订阅/公布机制,因此,使用这样的消息驱动模型的第一步便是订阅消息:而对Android应用程序来说,订阅消息事实上就是注冊广播接收器, ...

  10. 利用jquery来隐藏input type="file"

    <li> <input type="text" name="token" value = "<?php ech$_SESSIO ...