【leetcode】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
解题思路:动态规划的入门级题目。状态转移方程: dp[i][j] = min(dp[i-1][j],dp[i-1][j-1],dp[i-1][j+1]) + A[i][j]。
代码如下:
class Solution(object):
def minFallingPathSum(self, A):
"""
:type A: List[List[int]]
:rtype: int
"""
for i in range(len(A)):
for j in range(len(A[i])):
if i - 1 < 0:
continue
minv = A[i-1][j]
if j - 1 >= 0:
minv = min(minv,A[i-1][j-1])
if j + 1 < len(A[i]):
minv = min(minv,A[i-1][j+1])
A[i][j] = minv + A[i][j]
#print A
return min(A[-1])
【leetcode】931. Minimum Falling Path Sum的更多相关文章
- 【LeetCode】931. Minimum Falling Path Sum 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 相似题目 参考资料 日期 题目地址:htt ...
- 【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 ...
- 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】712. Minimum ASCII Delete Sum for Two Strings
题目如下: 解题思路:本题和[leetcode]583. Delete Operation for Two Strings 类似,区别在于word1[i] != word2[j]的时候,是删除word ...
- 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 ...
- 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】112. 路径总和 Path Sum 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 回溯 BFS 栈 日期 题目地址:https ...
随机推荐
- gensim word2vec |来自渣渣硕的学习笔记
最近写论文跑模型,要用到word2vec,但是发现自己怎么也看不懂网上的帖子,还是自己笨吧,所以就有了我的第一篇博客!!! 关于word2vec工具打算写一个系列的,当然今天这篇文章只打算写: 如何 ...
- webpack.config.js配置入口出口文件
目录结构: 新建webpack.config.js配置文件 const path = require('path') //导出 path是node内置的包 通过npm init初始化得到package ...
- UiAutomator、UiAutomator2、Bootstrap的关系
很多同学经过一段时间的学习之后都明白了Appium的基本原理,但是越学习到后面发现出现的很多陌生名词无法弄清楚其具体作用,今天这篇文章的目的就是为了让大家来弄懂三个高频名词:UiAutomator.U ...
- DELPHI FMX IOS模拟器调试时出现No SDKs could be found
解决办法: 在OSX里打开XCODE,点击XCODE菜单->Perferences->Locations在Commond Line Tools选择XCODE
- Java IO流总结(二)-示例
1.使用文件操作的9大方法完成文件的判断 判断文件或目录是否存在 : exits() 返回值boolean型 * 判断是否是文件: isFile() boolean * 判断是否是目录: isDiec ...
- linux系统下tomcat应用开机自启动 配置
linux系统下tomcat应用开机自启动 配置 相对简单的方式是将tomcat添加为系统服务第一步 复制文件将 $Tomcat_Home/bin目录下的 catalina.sh脚本文件复制到目录/ ...
- CPU C-States Power Saving Modes
http://www.hardwaresecrets.com/article/611 Everything You Need to Know About the CPU C-States Power ...
- Python 进阶_OOP 面向对象编程_self 的实例绑定
目录 目录 self 和绑定 调用非绑定的方法 self 和绑定 在 Python 中 self 变量是特殊的, 其用于在实例方法中引用该方法所绑定的实例, 换句话说就是 Python 在实例化对象时 ...
- 【Unity3D NGUI】----UI尺寸和位置的调整
1 尺寸与位置 通过UIWidget类获取,该类是所有UI元素的基类 在unity中创建一个sprite,如下图所示 这里面这个sprite的大小受几个属性的影响,首先是属性面板里的Size,对应的U ...
- Java总结之Java简介
一.序言 1.软件的介绍 软件是指一系列按照特定顺序组织的计算机数据和指令的集合. 2.人机交互 实现人与计算机的交互,主要有两种方式: 图形界面方式(Graphical User Interface ...