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二次开发-遍历当前part所有component,把装配子部件设置成工作部件
NX11+VS2013 #include <uf.h> #include <uf_disp.h> #include <uf_modl.h> #include < ...
- 秦曾昌人工智能课程---7、决策树集成学习Tree Ensembles
秦曾昌人工智能课程---7.决策树集成学习Tree Ensembles 一.总结 一句话总结: 其实机器模型减少variance的比较好的方式就是 多个模型取平均值 1.CART是什么? classi ...
- 20140316 window live write 插件 推荐代码高亮插件 构造函数只能用初始化成员列表方式的例子
1.window live write 插件:http://www.cnblogs.com/liuxianan/archive/2013/04/13/3018732.html 2.推荐代码高亮插件:W ...
- scala容器对象(转载)
1Array 数组 Scala的数组是这个样子: val arr = new Array[String](3) 程序员们基本都看得懂,new 一个Array对象,它的类型是String,长度为3.对元 ...
- 好文 | MySQL 索引B+树原理,以及建索引的几大原则
Java技术栈 www.javastack.cn 优秀的Java技术公众号 来源:小宝鸽 blog.csdn.net/u013142781/article/details/51706790 MySQL ...
- SpringMVC(AbstractController,拦截器,注解)
1.Controller接口及其实现类 Controller是控制器/处理器接口,只有一个方法handleRequest,用于进行请求的功能处理(功能处理方法),处理完请求后返回ModelAndVie ...
- docker集群管理之kubernetes
一.简介 kubernetes又叫做k8s,是Google开发的一款开源的docker集群管理工具,在这里对它的“发家史”,我不做过多的阐述,有时间大家可以自己去百度一下: 下面我要讲的就是容易混淆的 ...
- finalize的作用
1. finalize的作用 finalize()是Object的protected方法,子类可以覆盖该方法以实现资源清理工作,GC在回收对象之前调用该方法. finalize()与C++中的析构函数 ...
- PHP算法之盛最多水的容器
给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两条线, ...
- 【JZOJ6342】Tiny Counting
description analysis 首先不管\(a,b,c,d\)重复的情况方案数是正逆序对之积 如果考虑\(a,b,c,d\)有重复,只有四种情况,下面括号括起来表示该位置重复 比如\(\{a ...