Leetcode931. Minimum Falling Path Sum下降路径最小和
给定一个方形整数数组 A,我们想要得到通过 A 的下降路径的最小和。
下降路径可以从第一行中的任何元素开始,并从每一行中选择一个元素。在下一行选择的元素和当前行所选元素最多相隔一列。
示例:
输入:[[1,2,3],[4,5,6],[7,8,9]] 输出:12 解释: 可能的下降路径有:
- [1,4,7], [1,4,8], [1,5,7], [1,5,8], [1,5,9]
- [2,4,7], [2,4,8], [2,5,7], [2,5,8], [2,5,9], [2,6,8], [2,6,9]
- [3,5,7], [3,5,8], [3,5,9], [3,6,8], [3,6,9]
和最小的下降路径是 [1,4,7],所以答案是 12。
提示:
- 1 <= A.length == A[0].length <= 100
- -100 <= A[i][j] <= 100
典型的动态规划
当前最好的状态和上一层的状态有关。
const int INF = INT_MAX;
class Solution {
public:
int minFallingPathSum(vector<vector<int> >& A)
{
int r = A.size();
int c = A[0].size();
vector<vector<int> > dp(r, vector<int>(c, INF));
for(int i = 0; i < c; i++)
{
dp[0][i] = A[0][i];
}
for(int i = 1; i < r; i++)
{
for(int j = 0; j < c; j++)
{
if(j == 0)
{
dp[i][j] = min(dp[i - 1][j], dp[i - 1][j + 1]) + A[i][j];
}
else if(j == c - 1)
{
dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - 1]) + A[i][j];
}
else
{
dp[i][j] = min(dp[i - 1][j], min(dp[i - 1][j - 1], dp[i - 1][j + 1])) + A[i][j];
}
}
}
int ans = INF;
for(int i = 0; i < c; i++)
{
ans = min(ans, dp[r - 1][i]);
}
return ans;
}
};
因为当前索引的状态只跟上一层的上一个索引状态丶当前索引状态丶下一个索引状态有关。
用一个变量去维护上一层的上一个索引状态,然后再从前往后开始遍历。只需要一维的数组空间就可以了。
优化后:
const int INF = INT_MAX;
class Solution {
public:
int minFallingPathSum(vector<vector<int> >& A)
{
int r = A.size();
int c = A[0].size();
vector<int> dp(c, INF);
for(int i = 0; i < c; i++)
{
dp[i] = A[0][i];
}
for(int i = 1; i < r; i++)
{
int last = INF;
for(int j = 0; j < c; j++)
{
if(j == 0)
{
last = dp[j];
dp[j] = min(dp[j], dp[j + 1]) + A[i][j];
}
else if(j == c - 1)
{
dp[j] = min(dp[j], last) + A[i][j];
}
else
{
int temp = dp[j];
dp[j] = min(dp[j], min(last, dp[j + 1])) + A[i][j];
last = temp;
}
}
}
int ans = INF;
for(int i = 0; i < c; i++)
{
ans = min(ans, dp[i]);
}
return ans;
}
};
Leetcode931. Minimum Falling Path Sum下降路径最小和的更多相关文章
- [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. Minimum falling path sum 最小下降路径和(动态规划)
Leetcode 931. Minimum falling path sum 最小下降路径和(动态规划) 题目描述 已知一个正方形二维数组A,我们想找到一条最小下降路径的和 所谓下降路径是指,从一行到 ...
- Leetcode之动态规划(DP)专题-931. 下降路径最小和(Minimum Falling Path Sum)
Leetcode之动态规划(DP)专题-931. 下降路径最小和(Minimum Falling Path Sum) 给定一个方形整数数组 A,我们想要得到通过 A 的下降路径的最小和. 下降路径可以 ...
- LeetCode 931. Minimum Falling Path Sum
原题链接在这里:https://leetcode.com/problems/minimum-falling-path-sum/ 题目: Given a square array of integers ...
- [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 ...
- LeetCode 5129. 下降路径最小和 II Minimum Falling Path Sum II
地址 https://leetcode-cn.com/contest/biweekly-contest-15/problems/minimum-falling-path-sum-ii/ 题目描述给你一 ...
- 【LeetCode】931. Minimum Falling Path Sum 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 相似题目 参考资料 日期 题目地址:htt ...
- 108th LeetCode Weekly Contest 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 ...
- 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 ...
随机推荐
- NX二次开发-UFUN读取本地文本文档uc4514a
1 NX9+VS2012 2 3 #include <uf.h> 4 #include <uf_cfi.h> 5 #include <uf_ui.h> 6 7 us ...
- NX二次开发-UFUN设置视图边界线显示隐藏UF_DRAW_set_border_display
#include <uf.h> #include <uf_draw.h> #include <uf_drf.h> #include <uf_obj.h> ...
- ionic-CSS:ionic 按钮
ylbtech-ionic-CSS:ionic 按钮 1.返回顶部 1. onic 按钮 按钮是移动app不可或缺的一部分,不同风格的app,需要的不同按钮的样式. 默认情况下,按钮显示样式为:dis ...
- 利用IK分词器,自定义分词规则
IK分词源码下载地址:https://code.google.com/p/ik-analyzer/downloads/list lucene源码下载地址:http://www.eu.apache.or ...
- Unity实现Android端视频播放
本文只讲Android短的视频播放 实现方式 使用Handheld.PlayFullScreenMovie(),这个函数实现.具体如下: 1.创建StreamingAssets文件夹,此文件夹放入视频 ...
- Linux安全审计-基础篇
安全审计这块我能想到的有两种方案可以解决,一种是在Linux中配置实现,一种是使用Python开发堡垒机实现,我先实现了第一种比较简单的:后面会开发堡垒机: 一.首先我们需要在/etc/profi ...
- ES6 学习 -- let const
看过很多大佬的ES6笔记,总结一下学习后的收获,给自己当作一个笔记用用: ES3.ES5定义变量有两种方法:var 和 function ES6定义变量有var.function.let.const等 ...
- mysql UDF提权 sys_bineval
介绍: 执行shellcode – sys_bineval sqmapl自带udf.dll中存在函数’sys_bineval,执行shellcode – sys_bineval 利用: MSF生成sh ...
- python库之lightgbm
一.安装 https://blog.csdn.net/qq_40317897/article/details/81021958 参考文献: [1].LightGBM中文文档 https://light ...
- javascript 的学习笔记(第一天)
1.==与=== == 先转换类型,再比较 === 直接比较 2.parseInt 把字符串转成整数 parsefloat 把字符串转成小数 3. 变量的作用域:变量起作用的范围 局部变量: ...