原题链接在这里:https://leetcode.com/problems/minimum-falling-path-sum/

题目:

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

题解:

For each cell A[i][j], the minimum falling path sum ending at this cell = A[i][j]+ Min(minimum sum ending on its upper left, minimum sum ending on its upper, minimum sum ending on it upper right).

Could use dp to cash previous value.

Time Complexity: O(m*n). m = A.length. n = A[0].length.

Space: O(m*n).

AC Java:

 class Solution {
public int minFallingPathSum(int[][] A) {
if(A == null || A.length == 0 || A[0].length == 0){
return 0;
} int res = Integer.MAX_VALUE;
int m = A.length;
int n = A[0].length;
int [][] dp = new int[m+1][n]; for(int i = 1; i<=m; i++){
for(int j = 0; j<n; j++){
int leftUp = j==0 ? dp[i-1][j] : dp[i-1][j-1];
int rightUp = j == n-1 ? dp[i-1][j] : dp[i-1][j+1];
dp[i][j] = A[i-1][j] + Math.min(leftUp, Math.min(dp[i-1][j], rightUp));
if(i == m){
res = Math.min(res, dp[i][j]);
}
}
} return res;
}
}

Could operate on original A.

Time Complexity: O(m*n).

Space: O(1).

AC Java:

 class Solution {
public int minFallingPathSum(int[][] A) {
if(A == null || A.length == 0 || A[0].length == 0){
return 0;
} int res = Integer.MAX_VALUE;
int m = A.length;
int n = A[0].length; if(m == 1){
for(int j = 0; j<n; j++){
res = Math.min(res, A[0][j]);
} return res;
} for(int i = 1; i<m; i++){
for(int j = 0; j<n; j++){
int leftUp = j==0 ? A[i-1][j] : A[i-1][j-1];
int rightUp = j == n-1 ? A[i-1][j] : A[i-1][j+1];
A[i][j] += Math.min(leftUp, Math.min(A[i-1][j], rightUp));
if(i == m-1){
res = Math.min(res, A[i][j]);
}
}
} return res;
}
}

LeetCode 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 解题报告(Python)

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

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

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

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

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

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

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

随机推荐

  1. Delphi百度文字识别【支持通用文字识别、身份证识别、银行卡识别、驾驶证识别、行驶证识别、车牌识别等功能】

    作者QQ:(648437169) 点击下载➨Delphi百度文字识别          百度api文档 [Delphi百度文字识别]支持 通用文字识别.通用文字识别(高精度版).通用文字识别(含位置信 ...

  2. day50——js补充

    day50 前端内容回顾 HTML 标签分类 块级标签:div p h1-h6 form hr br ul li ol table标签 内联标签:span a img label input sele ...

  3. 「UR#6」懒癌

    「UR#6」懒癌 妈妈我居然看了六个小时题解,快救救乌干达的可怜儿童吧. 接下来开始膜官方题解: ​ 其实就算有上面两个结论也不是很好想到任意复杂度的做法,关键在于要想到一个人是怎么推断自己的狗是不是 ...

  4. springcolud 的学习(一),架构的发展史

    一.传统架构 传统的SSH架构,分为三层架构 web控制层.业务逻辑层.数据库访问层. 传统架构也就是单点应用,就是大家在刚开始初学JavaEE技术的时候SSH架构或者SSM架构,业务没有进行拆分,都 ...

  5. ThreadLocal简解

    ThreadLocal特点 ThreadLocal实现了线程间数据隔离,ThreadLocal的实例代表了一个线程局部的变量,每条线程都只能看到自己的值,并不会意识到其它的线程中也存在该变量.简单来说 ...

  6. vue请求简单配置

    简单记录一下vue的http请求配置相关 测试环境请求接口设置: 1.  config/dev.env.js添加: module.exports = merge(prodEnv, { NODE_ENV ...

  7. 关于定义变量名为"name"的坑!!!

    昨天下午没有什么工作可做,闲来无事就上博客园看看了,有个问题让我一直很纳闷. 直接上代码吧: 再用表达式创建函数时遇到的问题,这里的代码按照正常逻辑只有那个在变量定义后面的函数执行打印的值才是&quo ...

  8. Android为TV端助力之解析序列话的JSON

    解析json时报错default constructor not found. class............. 比如 public class MediaRepBean implements P ...

  9. Flink Runtime核心机制剖析(转)

    本文主要介绍 Flink Runtime 的作业执行的核心机制.本文将首先介绍 Flink Runtime 的整体架构以及 Job 的基本执行流程,然后介绍在这个过程,Flink 是怎么进行资源管理. ...

  10. MySQL Replication--复制基本工作原理

    复制工作原理(1) master将改变记录到二进制日志(binary log)中(这些记录叫做二进制日志事件,binary log events):(2) slave将master的binary lo ...