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.

做这题之前,建议先看一下Leetcode Triangle

这题看懂了,然后把矩阵右旋45度后,再看此题感觉很相似,

把斜次对角线(包括主对角线)看成行,然后按照Leetcode Triangle的思路去做即可

如矩阵(矩阵的行数为n,矩阵的列数为m)

1 1 1 1

1 1 1 1

1 1 1 1

1 1 1 1

对于每一行分别对于原始矩阵列的第1~m个元素

决策变量为dp[i][j], 表示左上角到达第i行第j个元素最短路径

动态转移方程为dp[i][j] = min(dp[i][j-1],dp[i-1][j]) + grid[i][j]

由于二维数组空间比较大,本题对空间进行优化

矩阵旋转45度后为

1

1   1

1  1  1

1   1   1   1

1   1   1

1   1

1

你会发现dp[i][j-1]和dp[i-1][j]处在同一行的相领元素,而他们之间下面的元素为dp[i][j],就类似Leetcode Triangle

由于上一行的信息在下一行后不会用到,故利用滚动数组的去解决

  1. int minPathSum(vector<vector<int> >& grid){
  2. if(grid.empty()) return ;
  3. int n = grid.size(), m = grid[].size();
  4. vector<int> dp(m+,INT_MAX);
  5. dp[] = ;
  6. for(int i = ; i < n; ++ i){
  7. for(int j = ; j < m ; ++ j){
  8. dp[j+] = min(dp[j],dp[j+]) + grid[i][j];
  9. }
  10. }
  11. return dp[m];
  12. }

Leetcode Minimum Path Sum的更多相关文章

  1. 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance

    引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...

  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] 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]Minimum Path Sum @ Python

    原题地址:https://oj.leetcode.com/problems/minimum-path-sum/ 题意: Given a m x n grid filled with non-negat ...

  5. LeetCode:Minimum Path Sum(网格最大路径和)

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

  6. LeetCode Minimum Path Sum (简单DP)

    题意: 给一个n*m的矩阵,每次可以往下或右走,经过的格子中的数字之和就是答案了,答案最小为多少? 思路: 比较水,只是各种空间利用率而已. 如果可以在原空间上作修改. class Solution ...

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

  8. [Leetcode Week9]Minimum Path Sum

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

  9. LeetCode 64. 最小路径和(Minimum Path Sum) 20

    64. 最小路径和 64. Minimum Path Sum 题目描述 给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明: 每次只能向下或 ...

随机推荐

  1. python 连接sql server

    linux 下pymssql模块的安装 所需压缩包:pymssql-2.1.0.tar.bz2freetds-patched.tar.gz 安装: tar -xvf pymssql-2.1.0.tar ...

  2. ps -C nginx --no-header |wc -l

    [root@ok ok]# ps --help|grep C -A all processes -C by command name -V,V show version L list format c ...

  3. XAML语言介绍

    <Window x:Class="WpfApplication1.Window1" xmlns="http://schemas.microsoft.com/winf ...

  4. RESTful架构入门

    理解RESTful架构 - 阮一峰的网络日志http://www.ruanyifeng.com/blog/2011/09/restful RESTful API 设计指南 - 阮一峰的网络日志http ...

  5. 【翻译十九】-java之执行器

    Executors In all of the previous examples, there's a close connection between the task being done by ...

  6. Object.create 函数 (JavaScript)

    创建一个具有指定原型且可选择性地包含指定属性的对象. 语法 Object.create(prototype, descriptors) 参数 prototype 必需.  要用作原型的对象.  可以为 ...

  7. [LeetCode] Ugly Number

    Ugly Number Total Accepted: 20760 Total Submissions: 63208 Difficulty: Easy Write a program to check ...

  8. opacity

    .css{filter:alpha(opacity:30);/*filter是给IE用到*/opacity:.3; }

  9. 【转】Linux终端下 dstat 监控工具

    转自https://linux.cn/article-3215-1.html dstat 是一个可以取代vmstat,iostat,netstat和ifstat这些命令的多功能产品.dstat克服了这 ...

  10. maven会报Could not transfer artifact xxx错误

    需要在你的eclipse更新一下maven的包 如下: