【LeetCode练习题】Minimum Path Sum

时间:2022-11-11 16:21:09

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位置的给定值。

特殊的是:

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

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

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

画图来理解:

【LeetCode练习题】Minimum Path Sum

代码如下:

空间复杂度: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. &lbrack;Leetcode Week9&rsqb;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&period; 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. &lbrack;LeetCode&rsqb; 64&period; 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&lpar;easy&rpar;

    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&num;解leetcode 64&period; 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. Linux makefile教程之后序十一&lbrack;转&rsqb;

    后序 —— 终 于到写结束语的时候了,以上基本上就是GNU make的Makefile的所有细节了.其它的产商的make基本上也就是这样的,无论什么样的make,都是以文件的依赖性为基础的,其基本是都 ...

  2. fastclick插件 导致 input&lbrack;type&equals;&quot&semi;date&quot&semi;&rsqb; 无法触发问题解决方案

    鄙人才疏学浅,新人一枚,不足之处还请谅解,写下这个也只是为了给大家分享一下我解决这个BUG的方法,也是自己的一个笔记. 首先,我们使用fastclick插件的初衷是解决“tap”事件“点透”的BUG: ...

  3. 安全框架Shiro和Spring Security比较

    Shiro 首先Shiro较之 Spring Security,Shiro在保持强大功能的同时,还在简单性和灵活性方面拥有巨大优势. Shiro是一个强大而灵活的开源安全框架,能够非常清晰的处理认证. ...

  4. mysql函数操作

    <?php try{ $dbh = new PDO('mysql:dbname=testdb;host=localhost', 'mysql_user', 'mysql_pwd'); }catc ...

  5. 第四节:dingo&sol;API 最新版 V2&period;0 之 Responses (连载)

    因为某些某些原因,不能按时更新,唉.我会尽力,加快速度.(这句话不是翻译的哈) 原文地址--> https://github.com/dingo/api/wiki/Responses A fun ...

  6. 应用wavesurfer&period;js绘制音频波形图小白极速上手总结

    一.简介 1.1  引   人生中第一份工作公司有语音识别业务,需要做一个web网页来整合语音引擎的标注结果和错误率等参数,并提供人工比对的语音标注功能(功能类似于TranscriberAG等),(博 ...

  7. element table 二次封装 父子组件传值 组件通信

    新建一个组件(即子组件)table.vue 子组件编辑内容如下图所示 子组件通过props获取父组件传递过来的参数,如下图所示,type指明传递到子组件的数据类型,default指定默认值,一般不给 ...

  8. 读取&period;Properties文件以及Spring注解读取文件内容

    public class Main { public static void main(String[] args) throws IOException { //创建Properties对象 Pro ...

  9. Oracle&&num;160&semi;修改oracle数据库名

    Oracle 修改oracle数据库名 by:授客 QQ:1033553122 1.确保你有个可用于数据库恢复的,完整的数据库备份 2.确保数据库处于mount,非open状态,并且在加载前先以imm ...

  10. 学习HTML 第三节&period;接近正题:HTML样式-CSS级联样式表

    CSS (Cascading Style Sheets)级联样式表 内联样式 内联样式- 在HTML元素中使用"style" 属性 使用内联样式的方法是在相关的标签中使用样式属性. ...