题目如下:

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. 1 <= A.length == A[0].length <= 100
  2. -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的更多相关文章

  1. 【LeetCode】931. Minimum Falling Path Sum 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 相似题目 参考资料 日期 题目地址:htt ...

  2. 【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 ...

  3. Leetcode 931. Minimum falling path sum 最小下降路径和(动态规划)

    Leetcode 931. Minimum falling path sum 最小下降路径和(动态规划) 题目描述 已知一个正方形二维数组A,我们想找到一条最小下降路径的和 所谓下降路径是指,从一行到 ...

  4. [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 ...

  5. LeetCode 931. Minimum Falling Path Sum

    原题链接在这里:https://leetcode.com/problems/minimum-falling-path-sum/ 题目: Given a square array of integers ...

  6. 【leetcode】712. Minimum ASCII Delete Sum for Two Strings

    题目如下: 解题思路:本题和[leetcode]583. Delete Operation for Two Strings 类似,区别在于word1[i] != word2[j]的时候,是删除word ...

  7. 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 ...

  8. 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 ...

  9. 【LeetCode】112. 路径总和 Path Sum 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 回溯 BFS 栈 日期 题目地址:https ...

随机推荐

  1. poj 2104: K-th Number 【主席树】

    题目链接 学习了一下主席树,感觉具体算法思路不大好讲.. 大概是先建个空线段树,然后类似于递推,每一个都在前一个“历史版本”的基础上建立一个新的“历史版本”,每个历史版本只需占用树高个空间(好神奇!) ...

  2. leetcode-15双周赛-1286-字母组合迭代器

    题目描述: 方法: class CombinationIterator: def __init__(self, characters: str, combinationLength: int): se ...

  3. 使用idea搭建Spring boot+jsp的简单web项目

    大家好: 这是我的第一篇博客文章,简单介绍一下Spring boot + jsp 的搭建流程,希望给跟我一样新接触Spring boot的读者一点儿启发. 开发工具:jdk1.8   idea2017 ...

  4. 【LeetCode 76】最小覆盖子串

    题目链接 [题解] 尺取法. 用l和r代表一个合法的覆盖子串. 我们不断地扩大右指针. 直到l..r包含T中的所有字母为止(重复的就要两次以上.) 然后我们可以尝试的让l++. 看看新的l..r是不是 ...

  5. 测开之路三十二:Flask基础之错误与重定向

    错误处理,框架默认的错误为:not Found 可以捕获,并自定义 准备一张自定义图片,放在static文件夹下,并在template下创建一个html文件,引用该图片 捕获404状态,返回自定义页面 ...

  6. 显示等待WebDriverWait+lambda

    代码,关键代码标红 参考文章:https://www.cnblogs.com/yoyoketang/p/6517477.html #coding:utf-8 ''' 这里写了一个百度搜索页的pageo ...

  7. The life-saving straw

    English learning   In contemporary world, English learning has gained great popularity and it is of ...

  8. Mr. Panda and Crystal(最短路+完全背包)

    http://codeforces.com/gym/101206/attachments 题意: T组输入,每组给出m,n,k,m为能量总数,n为水晶种类数,k为合成方案数.有的水晶可以用能量制造,有 ...

  9. 购物车1.0版——python第5天

    # 输出商品列表,用户输入序号,显示选中商品名称# 商品li = ['手机', '电脑', '耳机', '键盘', '鼠标']# 要求:1.页面显示序号+商品名称如下# 1 手机# 2 电脑# 3 耳 ...

  10. log4j基础配置使用

    添加log4j的jar包:可以从maven处下载:https://mvnrepository.com/artifact/log4j/log4j/1.2.17 <!-- https://mvnre ...