地址 https://leetcode-cn.com/contest/biweekly-contest-15/problems/minimum-falling-path-sum-ii/

题目描述
给你一个整数方阵 arr ,定义「非零偏移下降路径」为:从 arr 数组中的每一行选择一个数字,且按顺序选出来的数字中,相邻数字不在原数组的同一列。

请你返回非零偏移下降路径数字和的最小值。

示例 :

输入:arr = [[,,],[,,],[,,]]
输出:
解释:
所有非零偏移下降路径包括:
[,,], [,,], [,,], [,,],
[,,], [,,], [,,], [,,],
[,,], [,,], [,,], [,,]
下降路径中数字和最小的是 [,,] ,所以答案是 。 提示: <= arr.length == arr[i].length <=
- <= arr[i][j] <=

算法1
先暴力结果 tle了
动态规划试试

dp[i][j] = min(dp[i-1][0],dp[i-1][1],dp[i-1][2].......dp[i-1][y]); // 前提 y!= j

 class Solution {
public:
int dp[][]; int minsum( vector<vector<int>>& arr,int x,int y)
{
if(x<= ) return ;
int ret = ; for(int i = ;i<arr[].size();i++){
if(i != y){
ret = min(ret,dp[x-][i]);
}
} return ret;
} int minFallingPathSum(vector<vector<int>>& arr) {
for(int i = ; i < arr[].size();i++){
dp[][i] = arr[][i];
} for(int i = ;i< arr.size();i++){
for(int j =;j <arr[].size();j++){
dp[i][j] = arr[i][j] + minsum(arr, i,j);
}
} int ret = ;
for(int i = ;i<arr[].size();i++){
ret = min(ret,dp[arr.size()-][i]);
} return ret;
}
};

c++

LeetCode 5129. 下降路径最小和 II Minimum Falling Path Sum II的更多相关文章

  1. Leetcode之动态规划(DP)专题-931. 下降路径最小和(Minimum Falling Path Sum)

    Leetcode之动态规划(DP)专题-931. 下降路径最小和(Minimum Falling Path Sum) 给定一个方形整数数组 A,我们想要得到通过 A 的下降路径的最小和. 下降路径可以 ...

  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 下降路径最小和

    Given a square array of integers A, we want the minimum sum of a falling path through A. A falling p ...

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

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

  5. LeetCode 931. Minimum Falling Path Sum

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

  6. [Leetcode]931.下降路径最小和

    题目链接 这一题目首先需要弄懂题目的意思,下降路径最小和指的是在方阵中可以从上往下行走,走过后获得的值最小,方向可以是走左下,右下,直下. 题目和传统的动态规划一样,把边界的值先初始化,然后通过状态转 ...

  7. [Swift]LeetCode931. 下降路径最小和 | 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. Leetcode931. Minimum Falling Path Sum下降路径最小和

    给定一个方形整数数组 A,我们想要得到通过 A 的下降路径的最小和. 下降路径可以从第一行中的任何元素开始,并从每一行中选择一个元素.在下一行选择的元素和当前行所选元素最多相隔一列. 示例: 输入:[ ...

  9. LeetCode 931. 下降路径最小和 详解

    题目详情 给定一个方形整数数组 A,我们想要得到通过 A 的下降路径的最小和. 下降路径可以从第一行中的任何元素开始,并从每一行中选择一个元素.在下一行选择的元素和当前行所选元素最多相隔一列. 示例: ...

随机推荐

  1. How to: Recompile the Business Class Library 如何:重新编译业务类库

    The eXpressApp Framework supplies the Business Class Library that consists of three assemblies. eXpr ...

  2. 超宽banner图在版心居中

    步骤如下: 1.版心盒子设置相对定位relative 2.banner图设置绝对定位,设置block,清除默认的间距 3.banner图的left设置:left:50%:  margin-left:- ...

  3. npm 使用过程中报错问题-及npm使用

    原文地址:https://blog.csdn.net/u013022210/article/details/77740519 1.以下为报错具体详情:node 8.1.2 版本问题:其他空间安装成功但 ...

  4. 关于SQL2005备份和还原的语法操作

    原数据库:restore database 练习版 from disk='D:\db-recovery\@@@.BAK' WITH REPLACE/WITH NORECOVERY备份数据库:backu ...

  5. 快速排序 Vs. 归并排序 Vs. 堆排序——谁才是最强的排序算法

    知乎上有一个问题是这样的: 堆排序是渐进最优的比较排序算法,达到了O(nlgn)这一下界,而快排有一定的可能性会产生最坏划分,时间复杂度可能为O(n^2),那为什么快排在实际使用中通常优于堆排序? 昨 ...

  6. div块水平居中,垂直居中

    水平居中一个div想要水平居中于它的父div中只需要给它加css属性margin:0 auto; 即可 <!DOCTYPE html> <html> <head> ...

  7. cf 模拟

    https://codeforces.com/contest/1236/problem/D 题意:一个n*m格子矩阵,放一个人偶在左上角向右走,只能在每个格子最多右转一次,有k个障碍物.求是否能够一次 ...

  8. Python3字典update()方法

    描述 Python字典update()函数把字典参数dict2的key/value(键/值)对更新到字典dict里. update()方法语法: dict.update(dict2) 参数 dict2 ...

  9. Repeater嵌套

    我们自己观察 这是由两个重复项组成的 重复项包含重复项 而重复项的数据源是由订单号决定 即父Repeater的某数据源字段 protected void Repeater1_ItemDataBound ...

  10. 超级简单的数组加单链表实现Map

    /** * 超级简单的数组加单链表实现Map * @author jlj * */ public class MyHashMap { public MyList[] lists; public int ...