题目如下:

Given a square grid of integers arr, a falling path with non-zero shifts is a choice of exactly one element from each row of arr, such that no two elements chosen in adjacent rows are in the same column.

Return the minimum sum of a falling path with non-zero shifts.

Example 1:

Input: arr = [[1,2,3],[4,5,6],[7,8,9]]
Output: 13
Explanation:
The possible falling paths are:
[1,5,9], [1,5,7], [1,6,7], [1,6,8],
[2,4,8], [2,4,9], [2,6,7], [2,6,8],
[3,4,8], [3,4,9], [3,5,7], [3,5,9]
The falling path with the smallest sum is [1,5,7], so the answer is 13.

Constraints:

  • 1 <= arr.length == arr[i].length <= 200
  • -99 <= arr[i][j] <= 99

解题思路:记dp[i][j]为第i行取第j个元素时,在0~i行区间内获得的最小值。那么显然有dp[i][j] = min(dp[i][j],dp[i-1][k] + arr[i][j]) ,其中j != k。这样的话时间复杂度是O(n),超时了。再仔细想想,其实对于任意一个j,在dp[i-1]中只要找出最小值即可,当然最小值的所在的列不能和j相同。那么只需要记录dp[i-1]行中的最小值和次小值,如果最小值的下标和j相同就取次小值,否则取最小值。

代码如下:

class Solution(object):
def minFallingPathSum(self, arr):
"""
:type arr: List[List[int]]
:rtype: int
"""
dp = [[float('inf')] * len(arr) for _ in arr]
for i in range(len(arr)):
dp[0][i] = arr[0][i] def getMin(arr):
min_val = float('inf')
min_inx = 0
for i in range(len(arr)):
if min_val > arr[i]:
min_val = arr[i]
min_inx = i
return (min_val,min_inx) def getSecMin(arr,min_inx):
sec_min_val = float('inf')
sec_min_inx = 0
for i in range(len(arr)):
if i == min_inx:continue
if sec_min_val > arr[i]:
sec_min_val = arr[i]
sec_min_inx = i
return (sec_min_val,sec_min_inx) for i in range(1,len(arr)):
min_val, min_inx = getMin(dp[i-1])
sec_min_val, sec_min_inx = getSecMin(dp[i-1],min_inx)
for j in range(len(arr)):
if j == min_inx:
dp[i][j] = min(dp[i][j],dp[i-1][sec_min_inx] + arr[i][j])
else:
dp[i][j] = min(dp[i][j], dp[i - 1][min_inx] + arr[i][j])
return min(dp[-1])

【leetcode】1289. Minimum Falling Path Sum II的更多相关文章

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

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

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

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

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

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

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

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

  6. 【leetcode】Binary Tree Maximum Path Sum

    Binary Tree Maximum Path Sum Given a binary tree, find the maximum path sum. The path may start and ...

  7. 【leetcode】Binary Tree Maximum Path Sum (medium)

    Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...

  8. 【LeetCode】712. Minimum ASCII Delete Sum for Two Strings 解题报告(Python & C++)

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

  9. 【LeetCode】209. Minimum Size Subarray Sum 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/minimum- ...

随机推荐

  1. Python+requests重定向和追踪

    Python+requests重定向和追踪 一.什么是重定向 重定向就是网络请求被重新定个方向转到了其它位置 二.为什么要做重定向 网页重定向的情况一般有:网站调整(如网页目录结构变化).网页地址改变 ...

  2. docker login Harbor时报错403 Forbidden

    背景 在本地搭建了harbor后,在进行了相关配置后,还是报错:Error response from daemon: login attempt to http://10.xx.xx.xx:8000 ...

  3. 第8章:LeetCode--算法:二叉树的创建、遍历、删除、求高度

    创建> 需要给定一个root的key,所有小于这个key的放到左边,大于key的放到右边, 比如vector<int> tree = {5,2,7,1,9,3,8},最后的树: 5 ...

  4. 剑指offer32:把数组排成最小的数

    1 题目描述 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323. 2 思路 ...

  5. 复习二叉数 pat l2-006 数的遍历

    L2-006. 树的遍历   给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列.这里假设键值都是互不相等的正整数. 输入格式: 输入第一行给出一个正整数N(<=30),是二叉树中结点 ...

  6. c# redis密码验证笔记

    参考博客https://www.cnblogs.com/qukaicheng/p/7514168.html写的 安装教程https://www.redis.net.cn/tutorial/3503.h ...

  7. 使用css让表头固定的方法

    1.可以使用display: table; width: 100%; table-layout: fixed; table-layout: fixed;设置表格布局算法.tableLayout 属性用 ...

  8. python 利用pyttsx3文字转语音(转)

    原文链接作者 # -*- coding: utf-8 -*- import pyttsx3 engine = pyttsx3.init() with open("all.txt", ...

  9. adb实操

    一.命令 adb connect IP:5555 adb disconnect IP:5555 adb remount adb install 安装包的绝对路径 二.获取logcat信息 1.制作文件 ...

  10. 如何查找SAP Fiori launchpad Designer的准确路径即url地址

    比如我们知道在SPRO里下面这个路径的customizing activity里打开Fiori Launchpad designer: SAP Netweaver->UI technologie ...