原题链接在这里: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. python requests 超时与重试

    一 源起: requests模块作为python爬虫方向的基础模块实际上在日常实际工作中也会涉及到,比如用requests向对方接口url发送POST请求进行推送数据,使用GET请求拉取数据. 但是这 ...

  2. ubuntu 使用阿里云镜像源快速搭建kubernetes 1.15.2集群

    一.概述 搭建k8s集群时,需要访问google,下载相关镜像以及安装软件,非常麻烦. 正好阿里云提供了k8s的更新源,国内用户就可以直接使用了. 二.环境介绍 操作系统 主机名 IP地址 功能 配置 ...

  3. redis - redis安装与启动

    redis安装 下载redis安装包 wget http://download.redis.io/releases/redis-5.0.7.tar.gz 解压缩 tar -xzf redis-5.0. ...

  4. win10 mars xlog编译

    win10 mars xlog编译   一. 环境准备 安装 cmake 以及 python2.7, 以及下载 ndk-r16b,并配置环境变量 NDK_ROOT 指向 ndk 路径. 如果是 Win ...

  5. map小列

    // 有关学生信息的头文件student.h代码如下 #include #include using namespace std; struct Student                    ...

  6. Matlab适配器模式

    适配器模式是连接两个不兼容接口的桥梁,主要分为三种:类适配器.对象适配器以及接口适配器,本文根据https://blog.csdn.net/u012359453/article/details/791 ...

  7. C++字符串相互转换

    转自cs_wu原文 C++ char*,const char*,string的相互转换 1. string转const char* string s ="abc"; const c ...

  8. IDEA自动清理优化import包

    IDEA自动清理优化import包 直接上图: Add unambiguous imports on the fly:快速添加明确的导入. Optimize imports on the fly:快速 ...

  9. 自定义指令 VUE基础回顾7

    vue除了有v-if等内置指令,我们也可以创建自定义指令. 例:我们可以实现一个可以每隔一秒闪烁的节点,类似于<blink>标签的行为.添加一个指令类似于添加一个过滤器,可以将他传入vue ...

  10. 1行Python代码制作动态二维码

    原文地址 https://blog.csdn.net/m0_38106923/article/details/100603516 GitHub网站参见:https://github.com/sylns ...