LeetCode 5129. 下降路径最小和 II Minimum Falling Path Sum II

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

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

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

示例 1:

输入:arr = [[1,2,3],[4,5,6],[7,8,9]]
输出:13
解释:
所有非零偏移下降路径包括:
[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]
下降路径中数字和最小的是 [1,5,7] ,所以答案是 13 。

提示:

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

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

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

 1 class Solution {
 2 public:
 3     int dp[1000][1000];
 4
 5     int minsum( vector<vector<int>>& arr,int x,int y)
 6     {
 7         if(x<= 0) return 0;
 8         int ret = 9999999;
 9
10         for(int i = 0;i<arr[0].size();i++){
11             if(i != y){
12                 ret = min(ret,dp[x-1][i]);
13             }
14         }
15
16         return ret;
17     }
18
19     int minFallingPathSum(vector<vector<int>>& arr) {
20         for(int i = 0; i < arr[0].size();i++){
21             dp[0][i] = arr[0][i];
22         }
23
24         for(int i =1 ;i< arr.size();i++){
25             for(int j =0;j <arr[0].size();j++){
26                 dp[i][j] = arr[i][j] +  minsum(arr, i,j);
27             }
28         }
29
30
31         int ret = 9999999;
32         for(int i = 0;i<arr[0].size();i++){
33             ret = min(ret,dp[arr.size()-1][i]);
34         }
35
36         return ret;
37     }
38 };

c++

原文地址:https://www.cnblogs.com/itdef/p/12041662.html

时间: 2024-11-09 15:58:09

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

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

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

题目链接 这一题目首先需要弄懂题目的意思,下降路径最小和指的是在方阵中可以从上往下行走,走过后获得的值最小,方向可以是走左下,右下,直下. 题目和传统的动态规划一样,把边界的值先初始化,然后通过状态转移一步一步到最后一行 我们有dp[i][j]:意思是终点为(i,j)的下降路径最小值 状态方程为 dp[i][j] = min(dp[i-1][j-1], dp[i-1][j], dp[i-1][j+1])+A[i][j] ; 分别代表从左上(i-1,j-1),从正上方(i-1,j),从右上方(i-

[Swift Weekly Contest 108]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 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 t

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

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

[Leetcode]120.三角形路径最小和

---恢复内容开始--- 题目的链接 简单的动态规划题,使用了二维dp数组就能很好的表示. 由于有边界的问题,所以这个dp数组为 dp[n+1][n+1]. dp[i][j]意思是终点为(i-1,j-1)点的路径最小和. 我们需要把这个三角形变成方阵来看,先看看样例: [ [2], [3,4], [6,5,7], [4,1,8,3] ] 变成方阵之后就变成了 [ [2, INT_MAX,INT_MAX, INT_MAX], [3,               4,INT_MAX, INT_MAX

[Leetcode] Path Sum II路径和

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example:Given the below binary tree andsum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 题意:给定一数,在树中找出所有路径和等于该数的情况.方

【Leetcode】Path Sum II

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 思路:与[Leetcode]Path Sum 不同

LeetCode——Path Sum II

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 给定一个二叉树和一个值,找出所有根到叶的路径和等于