Given a square array of integers A, we want the minimum sum of a falling path through A.

A falling path starts at any element in the first row, and chooses one element from each row.  The next row's choice must be in a column that is different from the previous row's column by at most one.

Example 1:

Input: [[1,2,3],[4,5,6],[7,8,9]]
Output: 12
Explanation:
The possible falling paths are:
  • [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]

The falling path with the smallest sum is [1,4,7], so the answer is 12.

Note:

  1. 1 <= A.length == A[0].length <= 100
  2. -100 <= A[i][j] <= 100

Approath #1: Bottom to Top. [C++]

class Solution {
public int minFallingPathSum(int[][] A) {
int l = A.length;
int[][] dp = new int[l+1][l+1];
for (int i = 0; i < l; ++i)
for (int j = 0; j < l; ++j)
dp[i][j] = A[i][j];
for (int i = l-2; i >= 0; --i) {
for (int j = 0; j < l; ++j) {
int left = j > 0 ? dp[i+1][j-1] : Integer.MAX_VALUE;
int right = j < l-1 ? dp[i+1][j+1] : Integer.MAX_VALUE;
int down = dp[i+1][j];
dp[i][j] += Math.min(left, Math.min(down, right));
// System.out.print("dp[" + i + "][" + j + "]= " + dp[i][j] + " ");
}
// System.out.println();
} int ans = Integer.MAX_VALUE;
for (int i = 0; i < l; ++i)
ans = Math.min(ans, dp[0][i]); return ans;
}
}

  

Analysis:

Solving this problem using the thought of 'bottom to top', we calculate the minimum sum using dp[i][j] = dp[i][j] + min(left, right, down).

Finally,  we can find the answer at the first row.

931. Minimum Falling Path Sum的更多相关文章

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

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

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

  3. LeetCode 931. Minimum Falling Path Sum

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

  4. 【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 fal ...

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

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

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

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

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

  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. 【leetcode】1289. Minimum Falling Path Sum II

    题目如下: Given a square grid of integers arr, a falling path with non-zero shifts is a choice of exactl ...

随机推荐

  1. dede 复制文章,远程图片无法本地化

    解决方法: 1.找到dede的后台目录,在后台目录下的inc下找到inc_archives_functions.php 2.搜索GetCurContent函数,找到如下这段代码: preg_match ...

  2. Tomcat设置默认时区

    本文讲解如何在tomcat启动时设置JVM默认时区. 环境:JDK1.8.114 web容器:Tomcat 9 tomcat启动脚本 /etc/init.d/tomcat 操作系统ubuntu 16 ...

  3. Loadrunner12.5-录制http://www.gw.com.cn/网页时提示“SSL身份验证失败”错误,这是为什么呢?

    问题:LR产品,录制http://www.gw.com.cn/ 网页时提示下图错误,这是为什么呢? 请在如下recording options中选择正确的SSL版本,再进行录制. 注:如何确定那个SS ...

  4. 神奇的照片修复术,这才是 PS 的正确打开方式!

    蒲公英种子从远处飘回 聚成伞的模样 太阳从西边升起 落向东方 运动员回到起跑线上 轰鸣的火车退回家乡 雪花纷飞 飘向天际 我沉入梦乡 你还在我身旁 ——公益广告 大概只有时光倒流,我们才能回到那些每天 ...

  5. c++中如何定义编译期间常量,即这个常量可以用于定义数组下标

    在c++中,类里面的成员变量不仅仅可以被const修饰,还可以被static const修饰,此时一个内建类型(如int ,char ,long等)的static const 可以看做是一个编译期间的 ...

  6. sqlserver中set IDENTITY_INSERT on 和 off 的设置方法

    sqlserver中set IDENTITY_INSERT on 和 off 的设置方法: 执行插入数据库插入数据时报了以下错误,我明明没有给主键set值但还是报错 解决方法如下: qlserver ...

  7. 2018.07.04 POJ 1113 Wall(凸包)

    Wall Time Limit: 1000MS Memory Limit: 10000K Description Once upon a time there was a greedy King wh ...

  8. 1000多块整个插板,arduino + android 蓝牙插板的实现--屌丝版

       需求描述 儿子有一堆充电玩具,基本上都是锂电池,经常插上去充电忘了到时拔下来,所以需要一块能设置接通时间的插板以保障电池的安全.   硬件设计: 首先需要一块插板,接着需要一个继电器,然后采用a ...

  9. org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'testService' is defined

    org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'testService' is defi ...

  10. Java解决高并发方案(帮助你我他)

           一个小型的网站,可以使用最简单的html静态页面就实现了,配合一些图片达到美化效果,所有的页面均存放在一个目录下,这样的网站对系统架构.性能的要求都很简单.随着互联网业务的不断丰富,网站 ...