给定一个方形整数数组 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. iOS 点击Application icon加载推送通知Data

    今天做APNS远程推送通知遇到了一个问题,就是手机接收到通知的时候,如果马上点击通知的 alert view时候,系统马上唤醒你的Application,通知或调用你的didReceiveLocalN ...

  2. 信息安全-技术-Web:cookie

    ylbtech-信息安全-技术-Web:cookie Cookie,有时也用其复数形式 Cookies,指某些网站为了辨别用户身份.进行 session 跟踪而储存在用户本地终端上的数据(通常经过加密 ...

  3. day27-面向对象进阶

    #!/usr/bin/env python # -*- coding:utf-8 -*- # ----------------------------------------------------- ...

  4. 5、 postman的鉴权

    什么是鉴权? 鉴权(authentication)是指验证用户是否拥有访问系统的权利.常用的有两种鉴权方式,一种是session鉴权,一种是jwt鉴权,相对而言,后者居多. 实例: 比如有一个添加角色 ...

  5. Spring源码由浅入深系列六 CreateBean过程

  6. 20140319 const sizeof define 编译时分配内存

    1.面试宝典预处理,const,sizeof Define作用定义函数: //用一个宏定义FIND求一个结构体struc里某个变量相对于struc的偏移量,如FIND(student,a)//等于0 ...

  7. 基于SpringBoot的Swagger2快速入门

    1. Springboot 集成 Swagger2 1.1 导入Swagger2 依赖 <!-- https://mvnrepository.com/artifact/io.springfox/ ...

  8. USACO2005 Cow Acrobats /// 前缀和 oj23402

    题目大意: N (1 ≤ N ≤ 50,000)头牛叠罗汉 找到最优排序使所有牛的风险值总和最小 每头牛重量 (1 ≤ Wi ≤ 10,000) 力量 (1 ≤ Si ≤ 1,000,000,000) ...

  9. element-ui的layout将24等分换为48等分

    按住ctr箭点击element-ui/packages/theme-chalk/src/index";,再按住ctr贱点击col.scss跳转,将跳转到的col.scss中的24换为48(c ...

  10. 《构建之法》CH5~6读书笔记 PB16110698 第九周(~5.15)

    这段时间我阅读了<构建之法>的大部分章节,包括个人技能.软件测试.用户体验和需求分析等相关内容.之前的个人作业和结对作业结束后,我们的工作重心终于转向了团队项目,作为团队中前端组的组长,我 ...