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 ...
随机推荐
- 屏幕录像专家exe视频批量翻录成视频avi格式
笔者下载过一些视频,全部是屏幕录像专家录制的exe视频,这些视频没有密码,可以顺利打开看看. 有一日想把这些视频转换成wmv格式放到手机上查看.一个目录下有那么大exe视频.一个个转特别麻烦. 后来想 ...
- 只有自己看的懂的vue 二叉树的3级联动
我是在vue做的数据 actions mutations state index页面获取值 传递给子页面 子页面的操作 <template> <div class='cascade_ ...
- 注册带有Portal功能的DYN365_ENTERPRISE_PLAN1地址
使用官方进入的注册页面注册后试用,发现没有Portal功能. https://trials.dynamics.com/Dynamics365/Signup 使用以下的地址注册可以产生Portal ht ...
- C#的一些方法读程序转c++
1.Array.Copypublic static void Copy( Array sourceArray, int sourceIndex, Array destinationArray, int ...
- Jmeter将HTTP request报文体中的字符串转换为大写
<awd><client id='${__javaScript("${IndividualID}".toUpperCase())}'><member ...
- oracle 存储过程应用
1.查看 SELECT * FROM all_source WHERE type='PROCEDURE' and name=upper('liuyi_prcd'); 2.删除 DROP PROCEDU ...
- vue组件介绍
https://www.cnblogs.com/Leo_wl/p/5863185.html vue.js说说组件 什么是组件:组件是Vue.js最强大的功能之一.组件可以扩展HTML元素,封装可重 ...
- devart 放大招了
前面我纪念BDE 的文章里面说过,devart 会在今后在数据库存取技术上会有更大的 进步,没想到很快devart 放大招了.在最新的unidac 和sdac 中,devart 支持在非Windows ...
- python小练习--属性
箭头这个作业: class Box:#定义一个类名为Box,类名后不必有括号,类包含类属性和类方法,这个类没有定义类属性 '''这是一个计算体积的类'''#这是这个类的__doc__属性,执行类后就可 ...
- Eclipse出现An error has occurred,See error log for more details的错误
因为加入了Aptana组件所以一直报这个错误,用了cmd的方法依然不奏效,最后选择 Window > perferences > General > Startup and Shut ...