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 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
由于上一行的信息在下一行后不会用到,故利用滚动数组的去解决
int minPathSum(vector<vector<int> >& grid){
if(grid.empty()) return ;
int n = grid.size(), m = grid[].size();
vector<int> dp(m+,INT_MAX);
dp[] = ;
for(int i = ; i < n; ++ i){
for(int j = ; j < m ; ++ j){
dp[j+] = min(dp[j],dp[j+]) + grid[i][j];
}
}
return dp[m];
}
Leetcode Minimum Path Sum的更多相关文章
- 动态规划小结 - 二维动态规划 - 时间复杂度 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) 有关 这种情况下,时间 ...
- 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 ...
- [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 ...
- [leetcode]Minimum Path Sum @ Python
原题地址:https://oj.leetcode.com/problems/minimum-path-sum/ 题意: Given a m x n grid filled with non-negat ...
- LeetCode:Minimum Path Sum(网格最大路径和)
题目链接 Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right ...
- LeetCode Minimum Path Sum (简单DP)
题意: 给一个n*m的矩阵,每次可以往下或右走,经过的格子中的数字之和就是答案了,答案最小为多少? 思路: 比较水,只是各种空间利用率而已. 如果可以在原空间上作修改. class Solution ...
- [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 ...
- [Leetcode Week9]Minimum Path Sum
Minimum Path Sum 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/minimum-path-sum/description/ Descr ...
- LeetCode 64. 最小路径和(Minimum Path Sum) 20
64. 最小路径和 64. Minimum Path Sum 题目描述 给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明: 每次只能向下或 ...
随机推荐
- C语言中do...while(0)的妙用
在linux内核代码中,经常看到do...while(0)的宏,do...while(0)有很多作用,下面举出几个: 1.避免goto语句: 通常,如果一个函数开始要分配一些资源,然后如果在中途遇到错 ...
- C# 面试宝典
1.简述 private. protected. public. internal 修饰符的访问权限. private 私有成员 只有类成员才能访问 protected 保护成员 只有该类及该类的 ...
- 重温WCF之群聊天程序(十)
完成的效果图: 服务器端代码: using System; using System.Collections.Generic; using System.Linq; using System.Serv ...
- Pyqt QSS简单的Ui美化
什么是QSS QSS 是Qt StyleSheet 的简称,意思就是qt的样式表格,StyleSheet 可以像CSS一样的写样式.使页面美化跟代码层分开,利于维护. QSS的语法 同css一样,他也 ...
- 【翻译二】java--并发之进程与线程
Processes and Threads In concurrent programming, there are two basic units of execution: processes a ...
- 【叉积】【sdut 2508 图形密码】
图形密码 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 题目链接:http://acm.sdut.edu.cn/sdutoj/p ...
- centos6.4下安装php的imagick和imagemagick扩展教程
imagick在centos6.4的安装方法: .安装ImageMagick 代码如下: wget http://soft.vpser.net/web/imagemagick/ImageMagick- ...
- hdu 4411 2012杭州赛区网络赛 最小费用最大流 ***
题意: 有 n+1 个城市编号 0..n,有 m 条无向边,在 0 城市有个警察总部,最多可以派出 k 个逮捕队伍,在1..n 每个城市有一个犯罪团伙, 每个逮捕队伍在每个城市可以选 ...
- Java8中的default方法
default方法 Java 8中引入了一个新的概念,叫做default方法,也可以称为Defender方法,或者虚拟扩展方法(Virtual extension methods). Default方 ...
- python学习第三天
小结: 总体上,python是解释型语言,开源比较好,速度较慢,装逼神器,解释器较常用的是CPython,安装后python进入运行环境 exit()退出 第一个hello world : print ...