【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 ...
随机推荐
- APP前置代码
APP自动化前置代码: #导入包from appium import webdriverimport timedesired_caps = {}desired_caps['platformName'] ...
- 提高wifi速度的设置办法
系列的提高wifi速度的设置办法 在DNS一栏有你们家的地址,在你们家的地址前输入“114.114.114.114”并以“,”结尾(注意:要用英文输入法哦.) 设置完后点击左上角的“无线局域网”回到初 ...
- ARGB色彩模式
看到#ff ff ff 00 00这种 就是啦 .开头两位表示透明度.
- SQL查看所有表的大小
--查看所有表的大小 declare @id int ) declare @pages int declare @dbname sysname ,) ,) ,) create table #spt_s ...
- 20180914-Java实例03
Java 实例 – 字符串性能比较测试 以下实例演示了通过两种方式创建字符串,并测试其性能: // StringComparePerformance.java 文件 public class Stri ...
- 20175120彭宇辰 《Java程序设计》第十周学习总结
教材内容总结 十二章 Java多线程机制 一.进程与线程.操作系统与进程 -线程不是进程,是比进程更小的执行单位.但与进程不同的是,线程的中断和恢复可以更加节省系统的开销. -线程可以共享进程中的某些 ...
- LINUX shell脚本相关
调试脚本 测试脚本语法:bash -n file.sh 查看脚本每一步执行情况:bash -x file.sh 位置变量:$1,$2,... 特殊变量: %?:最后一个命令的执 ...
- DB-概念-数据库:数据库/Database
ylbtech-DB-概念-数据库:数据库/Database 数据库是以一定方式储存在一起.能与多个用户共享.具有尽可能小的冗余度.与应用程序彼此独立的数据集合,可视为电子化的文件柜——存储电子文件的 ...
- mongo 数据库存储
mongo 数据库,获取有赞的数据. from app import mongo from app.external.yz.goods_api import YzGoodsApi from openp ...
- CENTER OS7关闭防火墙
CentOS 7.0默认使用的是firewall作为防火墙,之前版本是使用iptables. 所以在CentOS 7执行下面命令是无法查看防火墙状态的. [root@localhost ~]# ser ...