给定一个方形整数数组 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. 1 <= A.length == A[0].length <= 100
  2. -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下降路径最小和的更多相关文章

  1. [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 ...

  2. Leetcode 931. Minimum falling path sum 最小下降路径和(动态规划)

    Leetcode 931. Minimum falling path sum 最小下降路径和(动态规划) 题目描述 已知一个正方形二维数组A,我们想找到一条最小下降路径的和 所谓下降路径是指,从一行到 ...

  3. Leetcode之动态规划(DP)专题-931. 下降路径最小和(Minimum Falling Path Sum)

    Leetcode之动态规划(DP)专题-931. 下降路径最小和(Minimum Falling Path Sum) 给定一个方形整数数组 A,我们想要得到通过 A 的下降路径的最小和. 下降路径可以 ...

  4. LeetCode 931. Minimum Falling Path Sum

    原题链接在这里:https://leetcode.com/problems/minimum-falling-path-sum/ 题目: Given a square array of integers ...

  5. [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 ...

  6. LeetCode 5129. 下降路径最小和 II Minimum Falling Path Sum II

    地址 https://leetcode-cn.com/contest/biweekly-contest-15/problems/minimum-falling-path-sum-ii/ 题目描述给你一 ...

  7. 【LeetCode】931. Minimum Falling Path Sum 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 相似题目 参考资料 日期 题目地址:htt ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. BZOJ随即跳题-随即到什么题你写什么题

    来挑战一下吧~ 请事先登录你BZOJ的账号!

  2. VIM 配色方案,先保存一下

    https://github.com/chriskempson/tomorrow-theme http://ethanschoonover.com/solarized http://ethanscho ...

  3. OSI七层网络模型分别是哪七层?各运行那些协议?

    本文摘自:https://blog.csdn.net/JeremyZJM/article/details/78184775 应用层 DHCP · DNS · FTP · Gopher · HTTP · ...

  4. Map获取键值,Map的几种遍历方法 (转载)

    Map类提供了一个称为entrySet()的方法,这个方法返回一个Map.Entry实例化后的对象集.接着,Map.Entry类提供了一个getKey()方法和一个getValue()方法,Map.E ...

  5. SVG动画制作工具 , 从此抛弃臃肿的gif

    VG简介 只要是程序员的你,你不得不知道svg图片,它可以无限任意放大拉伸都不会损失画质,就像系统字体一样可以无限矢量放大,svg更高级是可以用来制作矢量动画,现在各大浏览器和系统基本对svg已经支持 ...

  6. 2018-8-10-win10-uwp-如何让一个集合按照需要的顺序进行排序

    title author date CreateTime categories win10 uwp 如何让一个集合按照需要的顺序进行排序 lindexi 2018-08-10 19:16:50 +08 ...

  7. ASP.NET 页面的生命周期

    本文转载自清风飘过的博客,地址:http://www.cnblogs.com/couhujia/archive/2010/04/23/1718405.html 页面生命期分三个阶段:建立阶段,回发阶段 ...

  8. [NOIP2005] 过河【Dp,思维题,缩点】

    Online Judge:Luogu P1052 Label:Dp,思维题,缩点,数学 题目描述 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子 ...

  9. note : Get FilePathName from FILE_OBJECT

    转自:http://blog.csdn.net/lostspeed/article/details/11738311 封了一个函数, 从 FILE_OBJECT 中 得到 FilePathName 在 ...

  10. 校园商铺-2项目设计和框架搭建-7验证Dao

    以最简单的地区表为例 1.插入数据 insert into tb_area (area_name, priority) values('东苑', 1),('南苑', 7),('北苑', 5); sel ...