题目如下:

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:continueminv = 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 Areturn min(A[-1])

转载于:https://www.cnblogs.com/seyjs/p/9869071.html

【leetcode】931. Minimum Falling Path Sum相关推荐

  1. leetcode 931. Minimum Falling Path Sum(最小的下降路径和)

    下降的路径就是从第一行任一个元素出发,只能直着或斜着走到下一行,直到到达最后一行,返回形成的路径中最小的路径和. 思路: DP 从下往上推, 在最后一行选一个元素,那么它是从哪里过来的?是从上一行的j ...

  2. 【leetcode】963. Minimum Area Rectangle II

    题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...

  3. 【LeetCode】#39组合总和(Combination Sum)

    [LeetCode]#39组合总和(Combination Sum) 加粗样式 给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数 ...

  4. python棋盘最短路径_【leetcode】64. Minimum Path Sum 棋盘最短路径

    1. 题目 Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right ...

  5. 【leetcode】1007. Minimum Domino Rotations For Equal Row

    题目如下: In a row of dominoes, A[i] and B[i] represent the top and bottom halves of the i-th domino.  ( ...

  6. 【leetcode】416. Partition Equal Subset Sum

    题目如下: 解题思路:对于这种判断是否的题目,首先看看动态规划能不能解决.本题可以看成是从nums中任选i个元素,判断其和是否为sum(nums)/2,很显然从nums中任选i个元素的和的取值范围是[ ...

  7. 【Leetcode】1526. Minimum Number of Increments on Subarrays to Form a Target Array(配数学证明)

    题目地址: https://leetcode.com/problems/minimum-number-of-increments-on-subarrays-to-form-a-target-array ...

  8. 【Leetcode】1335. Minimum Difficulty of a Job Schedule

    题目地址: https://leetcode.com/problems/minimum-difficulty-of-a-job-schedule/ 给定一个长nnn正整数数组AAA和一个正整数ddd, ...

  9. 【LeetCode】871. Minimum Number of Refueling Stops 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 贪心算法 日期 题目地址:https://leetc ...

最新文章

  1. 黑马程序员___Java基础[02-Java基础语法](一)
  2. 开发者AI职业指南:CSDN《AI技术人才成长路线图V1.0》重磅发布
  3. python核心编程-第六章-个人笔记(一)
  4. url、base64、blob,三者之间的转化
  5. [SDOI2011] 计算器
  6. 全国计算机等级考试题库二级C操作题100套(第68套)
  7. 动态定时任务数据库获取方式
  8. C艹 指针和const的关系和注意事项(非常有意思)
  9. php 响应时间,PHP下解决ajax请求服务器响应时间过长问题
  10. 将txt格式的地图导入到ArcMap
  11. lm算法c语言,lm算法(lm算法原理介绍)
  12. 面经个人向(算法岗)
  13. 计算机算法分析与设计心得体会,算法设计与分析课程的心得体会
  14. hp android 计算器,惠普图形计算器(HP Prime Graphing Calculator)
  15. swoole安装教程人人商城互动直播通信服务启用教程
  16. HTML5常用的文本标签及css字体样式属性
  17. 63. 请简述构造方法和析构方法的作用?
  18. 便签里的文件怎么分享?怎么把便签里的内容弄成文件发送
  19. echarts饼状图中间显示数值加上名字
  20. navigationController中navigationBar 的设置

热门文章

  1. Struts2中Action各种转发类型
  2. wing ide 3.x 中文设置
  3. 转载:Asp.net 2.0 GridView数据导出Excel文件(示例代码下载)
  4. 4. 怎么在生活中提升专注力?
  5. 【Android】Service生命周期回顾
  6. javaEE(3)_servlet基础
  7. platform_get_resource的分析
  8. 菜鸟的学习之路(7) — 包(package)、Object类和String类
  9. 使用 AppFuse 快速构建 J2EE 应用
  10. AWS想到办法让Alexa能在毫秒内做出回复