[Leetcode]931.下降路径最小和
这一题目首先需要弄懂题目的意思,下降路径最小和指的是在方阵中可以从上往下行走,走过后获得的值最小,方向可以是走左下,右下,直下。
题目和传统的动态规划一样,把边界的值先初始化,然后通过状态转移一步一步到最后一行
我们有dp[i][j]:意思是终点为(i,j)的下降路径最小值
状态方程为
dp[i][j] = min(dp[i-1][j-1], dp[i-1][j], dp[i-1][j+1])+A[i][j] ;
分别代表从左上(i-1,j-1),从正上方(i-1,j),从右上方(i-1,j+1)三种情况。求这三种情况的最小值即可。
答案很明显就是最后一行的最小值了,我们只需要调用STL自带的min_element或者for循环遍历即可找到。
我提交的代码如下,超越了100%的提交用户
class Solution {
public:
inline int min3(const int a,const int b,const int c){
int ans=a;
if(b<ans)ans=b;
if(c<ans)ans=c;
return ans;
}
int minFallingPathSum(vector<vector<int>>& A) {
int t = A.size();
int dp[t][t]; for (int j = ; j < t; j++) {
dp[][j] = A[][j];
} for (int i = ; i < t; i++) { dp[i][] = min(dp[i-][], dp[i-][]) + A[i][];
dp[i][t - ] = min(dp[i-][t - ], dp[i-][t-]) + A[i][t - ]; int tmp = ;
for (int j = ; j < t - ; j++) {
dp[i][j] = min3(dp[i-][j + ], dp[i-][j-], dp[i-][j]) + A[i][j];
}
} return *min_element(dp[t - ], dp[t-]+t); }
};
这段代码还做了几个改进,
- 是求三个数的最小值时使用了自己编写的而不是algorithm库自带的min(a,min(b,c));减少几次函数调用
- 既然已经知道是方阵,我们不需要再去求参数中的A[0].size()了,直接求一个A.size()即可。
[Leetcode]931.下降路径最小和的更多相关文章
- LeetCode 931. 下降路径最小和 详解
题目详情 给定一个方形整数数组 A,我们想要得到通过 A 的下降路径的最小和. 下降路径可以从第一行中的任何元素开始,并从每一行中选择一个元素.在下一行选择的元素和当前行所选元素最多相隔一列. 示例: ...
- Leetcode之动态规划(DP)专题-931. 下降路径最小和(Minimum Falling Path Sum)
Leetcode之动态规划(DP)专题-931. 下降路径最小和(Minimum Falling Path Sum) 给定一个方形整数数组 A,我们想要得到通过 A 的下降路径的最小和. 下降路径可以 ...
- LeetCode 5129. 下降路径最小和 II Minimum Falling Path Sum II
地址 https://leetcode-cn.com/contest/biweekly-contest-15/problems/minimum-falling-path-sum-ii/ 题目描述给你一 ...
- [Leetcode]120.三角形路径最小和
---恢复内容开始--- 题目的链接 简单的动态规划题,使用了二维dp数组就能很好的表示. 由于有边界的问题,所以这个dp数组为 dp[n+1][n+1]. dp[i][j]意思是终点为(i-1,j- ...
- [LeetCode] 931. Minimum Falling Path Sum 下降路径最小和
Given a square array of integers A, we want the minimum sum of a falling path through A. A falling p ...
- [Leetcode][动态规划] 第931题 下降路径最小和
一.题目描述 给定一个方形整数数组 A,我们想要得到通过 A 的下降路径的最小和. 下降路径可以从第一行中的任何元素开始,并从每一行中选择一个元素.在下一行选择的元素和当前行所选元素最多相隔一列. 示 ...
- [Swift]LeetCode931. 下降路径最小和 | Minimum Falling Path Sum
Given a square array of integers A, we want the minimum sum of a falling path through A. A falling p ...
- Leetcode931. Minimum Falling Path Sum下降路径最小和
给定一个方形整数数组 A,我们想要得到通过 A 的下降路径的最小和. 下降路径可以从第一行中的任何元素开始,并从每一行中选择一个元素.在下一行选择的元素和当前行所选元素最多相隔一列. 示例: 输入:[ ...
- Leetcode 931. Minimum falling path sum 最小下降路径和(动态规划)
Leetcode 931. Minimum falling path sum 最小下降路径和(动态规划) 题目描述 已知一个正方形二维数组A,我们想找到一条最小下降路径的和 所谓下降路径是指,从一行到 ...
随机推荐
- Codeforces 1086 简要题解
文章目录 A题 B题 C题 D题 E题 传送门 这场比赛原地爆炸了啊!!! 只做了两道. A题 传送门 手贱没关freopenfreopenfreopen于是wawawa了一次,死活调不出错. 题意: ...
- 2018.10.29 NOIP训练 数据结构(带修改莫队)
传送门 带修莫队板题. 直接按照经典写法做就行了. 代码
- yyparse() and yylex()
Yacc 与 Lex 快速入门 yyparse() returns a value of 0 if the input it parses is valid according to the give ...
- [转]深入理解mysqldump原理
本文转至:http://blog.csdn.net/cug_jiang126com/article/details/49824471 在mysqldump过程中,之前其实一直不是很理解为什么加了--s ...
- Gitolite 权限控制
官网 http://gitolite.com/gitolite/index.html 安装配置 http://gitolite.com/gitolite/install/ 傻瓜安装教程 http:// ...
- Redis模块开发示例
实现一个Redis module,支持两个扩展命令: 1) 可同时对hash的多个field进行incr操作: 2) incrby同时设置一个key的过期时间 在没有module之前,需要借助eval ...
- Eclipse添加servlet-api.jar库的引用
右键Application-->Properties-->Java Build Path-->Libraries-->Add External JARs-->servle ...
- kepware http接口 shell开发
读取某变量的值(wget wget --quiet \ --method GET \ --header 'Connection: keep-alive' \ --header 'Cache-Contr ...
- 中国移动物联网平台数据转发 c# 控制台程序
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.N ...
- Scala_方法、函数、柯里化
方法.函数.柯里化 方法 声明方法: scala> def m1(x:Int,y:Int):Int = { | x + y | }m1: (x: Int, y: Int)Ints ...