原题链接在这里: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. Python3 - 数字类型

    在 Python 中,数字并不是一个真正的对象类型,而是一组类似类型的分类.Python 不仅支持通常的数字类型(整数和浮点数),而且还能够通过常量去直接创建数字以及处理数字的表达式.数字数据类型是不 ...

  2. AVR单片机教程——EasyElectronics Library v1.2手册

    索引: bit.h delay.h pin.h wave.h pwm.h led.h rgbw.h button.h switch.h segment.h 主要更新: 添加了segment.h的文档: ...

  3. Linux基础(03)gdb调试

    1. 安装GDB增强工具 (gef) * GDB的版本大于7.7 * wget -q -O- https://github.com/hugsy/gef/raw/master/scripts/gef.s ...

  4. leetcode动态规划笔记二

    动态规划 题目分类 一维dp 矩阵型DP Unique Paths II : 矩阵型DP,求所有方法总数 Minimum Path Sum:矩阵型,求最大最小值 Triangle : 矩阵型,求最大最 ...

  5. Docker容器跨主机通信之:OVS+GRE

    一.概述 由于docker自身还未支持跨主机容器通信,需要借助docker网络开源解决方案 OVS OpenVSwich即开放式虚拟交换机实现,简称OVS,OVS在云计算领域应用广泛,值得我们去学习使 ...

  6. Django模板语言的学习

    1.模板系统 1.语法 1.变量相关 {{ name}} ,{{ name|length}}, {{ name |default:"默认值"}} 2.逻辑相关 1.if判断 {% ...

  7. SP375 QTREE - Query on a tree (树剖)

    题目 SP375 QTREE - Query on a tree 解析 也就是个蓝题,因为比较长 树剖裸题(基本上),单点修改,链上查询. 顺便来说一下链上操作时如何将边上的操作转化为点上的操作: 可 ...

  8. 01、MySQL_简介

    数据库概念 数据库(Database)是按照数据结构来组织.存储和管理数据的建立在计算机存储设备上的仓库. 数据库:存储数据的仓库 数据库分类 网络数据库 网络数据库是指把数据库技术引入到计算机网络系 ...

  9. Linux系统:保证数据安全落盘

    在很多IO场景中,我们经常需要确保数据已经安全的写到磁盘上,以便在系统宕机重启之后还能读到这些数据.但是我们都知道,linux系统的IO路径还是很复杂的,分为很多层,每一层都可能会有buffer来加速 ...

  10. pandas-10 pd.pivot_table()透视表功能

    pandas-10 pd.pivot_table()透视表功能 和excel一样,pandas也有一个透视表的功能,具体demo如下: import numpy as np import pandas ...