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 ...
随机推荐
- 泛型约束where条件的使用(可以通过类型参数动态反射创建实例)
定义抽象的人类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...
- 2018.09.27 bzoj2510: 弱题(概率dp+循环矩阵优化)
传送门 简单概率dp. 显然每次转移的式子可以用一个矩阵表示出来: 这个是循环矩阵. 因此只用维护第一行快速幂一波就行了. 代码: #include<bits/stdc++.h> #def ...
- 2018.09.27 bzoj2118: 墨墨的等式(最短路+背包)
传送门 好题啊. 首先找到最小的一个非零系数记做a1a_1a1,然后如果WWW modmodmod a1=W′a_1=W'a1=W′ modmodmod a1a_1a1,且WWW是方程的一个可行 ...
- 2018.09.23 孙悟空大战鲤鱼精(单调队列优化dp)
描述 孙悟空大战鲤鱼精,孙悟空在通天河遇到鲤鱼精,他嫉恶如仇,看见妖精就手痒(忘了自己是妖精).但是鲤鱼精知道孙悟空的厉害,在孙悟空来到通天河,鲤鱼精就跑到了河对面.于是孙悟空就去追鲤鱼精. 我们可以 ...
- 一个 图片 滚动 飞入的css特效
@keyframes bounceInLeft { from, 60%, 75%, 90%, to {animation-timing-function: cubic-bezier(0.215, 0. ...
- Radius 中 与Response Authernticator 与 Message-Authenticator的计算
/* String RequestStr3 = @"01 00 00 9E EB B2 E8 D9 1E 52 10 03 FB E1 52 39 27 58 93 F0 01 0E 33 ...
- SQL之经典SQL语句大全
经典SQL语句大全 一.基础 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname3.说明:备份sql serv ...
- php文件上传代码解析
php文件上传代码解析 is_uploaded_file() //函数判断指定的文件是否是通过 HTTP POST 上传的,返回一个布尔值. $_FILES['upfile']['tmp_name' ...
- redis只加载AOF文件
如果同时配置写AOF和RDB两种文件,但在redis启动时,只会加载AOF,除非配置只写RDB,才会加载RDB文件,也因此AOF文件必须是全量数据,所以会越来越大,这缺点也将是redis优化的一个方向 ...
- Android中Activity启动过程探究
首先追溯到Activity的启动,随便启动一个自己写的demo项目,使用DDMS进行debug标记,然后在Debug中把主线程暂停,可以看到调用栈.如下图所示: 于是我们先看android.app.A ...