给定一个方形整数数组 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. hdu多校第一场1005(hdu6582)Path 最短路/网络流

    题意: 在无向图上删边,让此图上从起点到终点的最短路长度变大,删边的代价是边长,求最小代价. 题解: 先跑一遍迪杰斯特拉,求出所有点的d[]值,然后在原图上保留所有的边(i,j)仅当i,j满足d[j] ...

  2. ionic-Javascript:ionic 上拉菜单(ActionSheet)

    ylbtech-ionic-Javascript:ionic 上拉菜单(ActionSheet) 1.返回顶部 1. ionic 上拉菜单(ActionSheet) 上拉菜单(ActionSheet) ...

  3. jQuery 基本使用

    index.html <head><meta http-equiv="Content-Type" content="text/html; charset ...

  4. 7 Scatter-loading Features

    7.1 About scatter-loading The scatter-loading mechanism enables you to specify the memory map of an ...

  5. 6 Accessing and Managing Symbols with armlink

    6.4 Image$$ execution region symbols The linker generates Image$$ symbols for every execution region ...

  6. VB.NET利用正則表達式巧妙限制字符输入

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u010028869/article/details/37913867     在通常的程序设计中.对 ...

  7. css元素垂直居中

    一.碎碎念:啊啊啊,原谅我只能起一个酱紫微大众微俗气的标题,因为实在没有什么能比这样表达的更清楚直观了呢! 二.没有知识储备,直接上示例: 1.思路:给父元素添加display: table属性:给子 ...

  8. flink widow&window funcion&水印

    在定义了窗口分配器之后,我们需要为每一个窗口明确的指定计算逻辑,这个就是窗口函数要做的事情, 当系统决定一个窗口已经准备好执行之后,这个窗口函数将被用来处理窗口中的每一个元素(可能是 分组的). 谁可 ...

  9. 6_再次开中断STI的正确姿势

    1 直接开启sti --蓝屏 2 配置环境 正确开启sti 中断 kpcr -- 很多重要线程切换的数据.结构 进入内核的时候 fs 不再是teb/tib: 是kpcr. 同时观察 kifastcal ...

  10. python 通过zabbix api获得所有主机的ip

    #!/usr/bin/env python3 #coding=utf-8 import jsonimport requests#from urllib import requests, parse,e ...