原题链接在这里: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. 山峰和山谷 Ridges and Valleys

    题目描述 思路 一开始看这道题目,也不是很会,谁会把统计之类的问题和bfs联系在一起,没有开始的状态,没有结束的状态,题目中连一个最短之类的词也没有出现. 然后统计嘛,题目中说了方格高度都相同,就把周 ...

  2. svg 直线水平渐变为什么没有效果,必须得是一条倾斜的不水平的直线才有渐变效果呢??

    参考:https://blog.csdn.net/u012260672/article/details/80905631 对x1=x2(没有宽度)或者y1=y2(没有高度)的直线(line以及path ...

  3. maven系列:archetype项目模板_create-from-project

    主要介绍create-from-project插件在命令行下的使用. [第一步:生成模板项目] 新建一个maven项目,比如叫 :groupId=com.abc.demo,artifactId=com ...

  4. SnowflakeIdWorker

    /** * Twitter_Snowflake<br> * SnowFlake的结构如下(每部分用-分开):<br> * 0 - 0000000000 0000000000 0 ...

  5. c#中关于Convert.ToDouble的一个注意事项

    今天在写代码的时候被一个小细节坑了,以前没注意,现在才发现,代码如下: private void btnChangeCartonID_Click(object sender, EventArgs e) ...

  6. Fiddler-在fiddlerscript中修改某个请求的请求头内容

    1.进入  OnBeforeRequest函数 在里面添加如下代码 if(oSession.fullUrl.Contains("url")){// //headers中添加测试参数 ...

  7. 设置断点调式 fiddler

    1. 用IE 打开博客园的登录界面  http://passport.cnblogs.com/login.aspx 2. 打开Fiddler,  在命令行中输入bpu http://passport. ...

  8. Charles关于Https SSLHandshake解决备忘录

    抓包Https时错误提示:SSLHandshake: Received fatal alert: unknown_ca   1.准备工作,下载Charles版本 有情链接,提取码为:ghc6,其中包含 ...

  9. vue-quill-editor回显时移除焦点

    直接复制开用 解决在回显数据的时候会默认聚焦 this.$refs.myQuillEditor.quill.enable(false); setTimeout(() => { this.$ref ...

  10. MySQL Percona Toolkit--pt-osc执行SQL命令

    pt-osc执行日志 在对数据量为100000的表tb004做DROP COLUMN操作,pt-osc工具日志为: Operation, tries, wait: analyze_table, , c ...