【LeetCode】931. Minimum Falling Path Sum 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/minimum-falling-path-sum/description/
题目描述
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 <= A.length == A[0].length <= 100-100 <= A[i][j] <= 100
题目大意
从最上面一行开始向下走,每次移动的时候最多只可以移动一列。也就是说每次必须向下走一行,列可以不变、也可以向左右移动一列。求到达最后一行的时候,最短的路径长度。
解题方法
动态规划
刚做过类似的题目,但是我还是没有做出来。。这个题和799香槟塔很像,都是二维空间求最大、最小的路径问题。

如果看上面这个图就明白了,数组中每个位置都要从上一层获得三个相邻列的最小值,换句话说,每个位置都可以给下面三个相邻列传递最小值。那么,其实就是一个动态规划嘛,到每个位置的最短路径,就是当前数值加上到达上面那层的三个相邻列的最小值。
所以这个题代码其实很简单,只需要设置好边界,然后我们每次查找上面的三个最小值加上当前的位置,得到的就是到达当前位置的最小路径。
做DP的时候,不要怕设置边界条件。我以前总想着用各种方法想着让dp数组和原来的数组一样大,这个思想是错误的!因为我们记忆化搜索的时候实际上有很多边界条件的,其实是可以转化成dp的边界条件,或者说是初始条件。提前给dp数组设定各种边界条件,能简化很多状态转移代码~这个题就很好的说明了这点!
时间复杂度是O(MN),空间复杂度是O(MN)。
class Solution(object):
def minFallingPathSum(self, A):
"""
:type A: List[List[int]]
:rtype: int
"""
M, N = len(A), len(A[0])
dp = [[0] * (N + 2) for _ in range(M)]
for i in range(M):
dp[i][0] = dp[i][-1] = float('inf')
for j in range(1, N + 1):
dp[i][j] = A[i][j - 1]
for i in range(1, M):
for j in range(1, N + 1):
dp[i][j] = A[i][j - 1] + min(dp[i - 1][j - 1], dp[i - 1][j], dp[i - 1][j + 1])
return min(dp[-1])
相似题目
799. Champagne Tower
【面试现场】如何编程获得最多的年终红包奖?
参考资料
日期
2018 年 10 月 28 日 —— 啊,悲伤的周赛
【LeetCode】931. Minimum Falling Path Sum 解题报告(Python)的更多相关文章
- 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 ...
- 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: Binary Tree Maximum Path Sum 解题报告
Binary Tree Maximum Path SumGiven a binary tree, find the maximum path sum. The path may start and e ...
- Leetcode之动态规划(DP)专题-931. 下降路径最小和(Minimum Falling Path Sum)
Leetcode之动态规划(DP)专题-931. 下降路径最小和(Minimum Falling Path Sum) 给定一个方形整数数组 A,我们想要得到通过 A 的下降路径的最小和. 下降路径可以 ...
- LeetCode: Minimum Path Sum 解题报告
Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...
- 【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 ...
随机推荐
- JavaScript获取html表单值验证后跳转网页中的关键点
关键代码: 1.表单部分 <form action="Depart.jsp" name="myform" method="post" ...
- 了解 Linkerd Service Mesh 架构
从较高的层次上看,Linkerd 由一个控制平面(control plane) 和一个 数据平面(data plane) 组成. 控制平面是一组服务,提供对 Linkerd 整体的控制. 数据平面由在 ...
- winXP 下安装python3.3.2
1. 安装python-3.3.2 2. 安装setuptools 下载解压后,进入路径 python setup.py install 3.安装pip 下载解压后,进入路径 python setup ...
- Linux学习 - 挂载命令
一.mount 1 功能 将外设手工挂载到目标挂载点 2 语法 mount [-t 文件系统] [设备文件名] [挂载点] 3 范例 mkdir /mnt/cdrom 在/mnt下创建一个cd ...
- Spring.DM web开发环境搭建
作为一个初学者来说,搭建好Spring.DM 的web开发环境还是有些麻烦的.我就遇到了N多麻烦,走了很多弯路.本文介绍了2种比较简单的搭建Spring.DM OSGi web开发环境的搭建. 第 ...
- Dubbo消费者异步调用Future使用
Dubbo的四大组件工作原理图,其中消费者调用提供者采用的是同步调用方式.消费者对于提供者的调用,也可以采用异步方式进行调用.异步调用一般应用于提供者提供的是耗时性IO服务 一.Future异步执行原 ...
- 会话-cookie
package com.hopetesting.cookie;import javax.servlet.ServletException;import javax.servlet.annotation ...
- [MySQL实战-Mysql基础篇]-mysql的日志
参考文章: https://www.cnblogs.com/f-ck-need-u/archive/2018/05/08/9010872.html https://dev.mysql.com/doc/ ...
- 【Git】【Gitee】通过git远程删除仓库文件
安装Git Git安装配置-菜鸟教程 没有安装下载的,请读者自行安装下载. 启动与初步配置 配置用户名与邮箱 git config --global user.name "用户名" ...
- Python的安装和卸载
一.Python的安装,去Python官网https://www.python.org/downloads/下载对应安装包 进入官网后鼠标移动到downloads处然后点击Windows 二.安装包下 ...