931. Minimum Falling Path Sum
Given a square array of integers
A, we want the minimum sum of a falling path throughA.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 is12.
Note:
1 <= A.length == A[0].length <= 100-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的更多相关文章
- Leetcode 931. Minimum falling path sum 最小下降路径和(动态规划)
Leetcode 931. Minimum falling path sum 最小下降路径和(动态规划) 题目描述 已知一个正方形二维数组A,我们想找到一条最小下降路径的和 所谓下降路径是指,从一行到 ...
- [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 ...
- LeetCode 931. Minimum Falling Path Sum
原题链接在这里:https://leetcode.com/problems/minimum-falling-path-sum/ 题目: Given a square array of integers ...
- 【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 ...
- 【LeetCode】931. Minimum Falling Path Sum 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 相似题目 参考资料 日期 题目地址:htt ...
- Leetcode之动态规划(DP)专题-931. 下降路径最小和(Minimum Falling Path Sum)
Leetcode之动态规划(DP)专题-931. 下降路径最小和(Minimum Falling Path Sum) 给定一个方形整数数组 A,我们想要得到通过 A 的下降路径的最小和. 下降路径可以 ...
- [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 ...
- 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 ...
- 【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 ...
随机推荐
- js 获取高度
网页可见区域宽 :document.body.clientWidth; 网页可见区域高:document.body.clientHeight; 网页可见区域高:document.body.offs ...
- jquery获取radio单选框的值
1.获取原有单选框的值 var value=$("input[name='is_setting']:checked").val(); 2.获取重选后的单选框的值 <tr> ...
- hibernate4 , spring3 使用 org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean 报错 Implementing class
错误代码如下 Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with ...
- linux 硬盘分区攻略
以下的sdX代表硬盘分区(如sda1,sda2,sdb1...等等),如果已有的硬盘分区需要改变大小的话,请参考另一篇文章. /boot:开机用的磁盘空间了,至少78MB,一般给100MB就好了. / ...
- [开源,学习,分享]UWP第三方简书客户端分享
简介 Windows10正式版发布到现在,我利用零零碎碎的一些时间对UWP进行一些学习,也基于这门技术开发了一个第三方的简书App. 基本界面 优酷视频: http://v.youku.com/v_s ...
- KBMMW 4.80.00 发布
一大波更新来了. 4.80.00 March 30 2015 Important notes (changes that may break existing code) ======= ...
- 客户被绑,蒙眼,惊问:“想干什么?” 对方不语,鞭笞之,客户求饶:“别打,要钱?” 又一鞭,“十万够不?” 又一鞭,“一百万?” 又一鞭。客户崩溃:“你们TMD到底要啥?” “要什么?...
1. 客户被绑,蒙眼,惊问:“想干什么?” 对方不语,鞭笞之,客户求饶:“别打,要钱?” 又一鞭,“十万够不?” 又一鞭,“一百万?” 又一鞭.客户崩溃:“你们TMD ...
- 初探Java反射机制
反射库提供了一个非常丰富且精心设计的工具集,以便编写能够动态操纵java代码的程序库.这项功能被大量地应用于JavaBeans中.反射机制提供了在运行状态中获得和调用修改任何一个类的属性和方法的能力. ...
- 动态渲染的input怎么取消记忆功能
方法1 :自定义去除记忆功能属性: $('#index_table_filter > label > input[type="search"]').attr('auto ...
- web service 项目 和 普通 web项目 的 区别
web service 面向的是开发者(需要再次开发) 普通web 面向的是用户(直接使用)