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 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. 说明: 每次只能向下或 ...
随机推荐
- ViewPager部分源码分析二:FragmentManager对Fragment状态的管理完成ViewPager的child添加或移出
ViewPager维护child代码流程: 注:PagerAdapter 使用的是FragmentPagerAdapter类或者它的子类. viewPager.populate(): void pop ...
- Linux内核驱动之GPIO子系统(一)GPIO的使用
转自:http://blog.csdn.net/mirkerson/article/details/8464290 一 概述 Linux内核中gpio是最简单,最常用的资源(和 interrupt , ...
- gcc参数-l传递顺序错误导致`undefined reference'的一点小结
刚才编译一个pthread的单文件程序, 使用的命令行是: gcc -o thread1 -lpthread thread1.c 结果报错: $ gcc -o thread1 -lpthread th ...
- 【openGL】画正弦函数图像
#include "stdafx.h" #include <GL/glut.h> #include <stdlib.h> #include <math ...
- hibernate中many-to-one关联时出现ObjectNotFoundException异常
采用多对一关联,如果一的那端删除了,多的这端无法感知,虽然数据库中可以通过外键配置将多的一端置空,可是在hibernate里面我暂时不知道如何处理. 目前采用的方式: 1.首先,数据库中需要配置好外键 ...
- WebRTC代码走读(八):代码目录结构
转载注明出处http://blog.csdn.net/wanghorse ├── ./base //基础平台库,包括线程.锁.socket等 ├── ./build //编译脚本,gyp ├── ./ ...
- WebRTC音视频引擎研究(1)--整体架构分析
WebRTC技术交流群:234795279 原文地址:http://blog.csdn.net/temotemo/article/details/7530504 1.WebRTC目的 ...
- OpenCv实现两幅图像的拼接
直接贴上源码 来源:http://www.myexception.cn/image/1498389.html 实验效果 Left.jpg right.jpg ImageMatch.jpg #inclu ...
- 【设计模式】MVC模式
MVC 模式代表 Model-View-Controller(模型-视图-控制器) 模式.这种模式用于应用程序的分层开发. Model(模型) - 模型代表一个存取数据的对象或 JAVA POJO.它 ...
- UVA 12232 Exclusive-OR(并查集+思想)
题意:给你n个数,接着三种操作: I p v :告诉你 Xp = v I p q v :告诉你 Xp ^ Xq = v Q k p1 p2 … pk:问你k个数连续异或的结果 注意前两类操作可能会出现 ...