[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-1,j+1)三种情况。求这三种情况的最小值即可。

答案很明显就是最后一行的最小值了,我们只需要调用STL自带的min_element或者for循环遍历即可找到。

我提交的代码如下,超越了100%的提交用户

class Solution {
public:
    inline int min3(const int a,const int b,const int c){
        int ans=a;
        if(b<ans)ans=b;
        if(c<ans)ans=c;
        return ans;
    }
    int minFallingPathSum(vector<vector<int>>& A) {
      int t = A.size();
      int dp[t][t];

      for (int j = 0; j < t; j++) {
        dp[0][j] = A[0][j];
        }

        for (int i = 1; i < t; i++) {

            dp[i][0] = min(dp[i-1][0], dp[i-1][1]) + A[i][0];
            dp[i][t - 1] = min(dp[i-1][t - 1], dp[i-1][t-2]) + A[i][t - 1];

            int tmp = 0;
            for (int j = 1; j < t - 1; j++) {
                dp[i][j] = min3(dp[i-1][j + 1], dp[i-1][j-1], dp[i-1][j]) + A[i][j];
            }
        }

        return *min_element(dp[t - 1], dp[t-1]+t);

    }
};

这段代码还做了几个改进,

  1. 是求三个数的最小值时使用了自己编写的而不是algorithm库自带的min(a,min(b,c));减少几次函数调用
  2. 既然已经知道是方阵,我们不需要再去求参数中的A[0].size()了,直接求一个A.size()即可。

原文地址:https://www.cnblogs.com/adamwong/p/10205102.html

时间: 2024-10-08 16:33:47

[Leetcode]931.下降路径最小和的相关文章

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 解释: 所有非零偏移

[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

[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:Triangle 三角形塔最小路径和

Triangle: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to botto

Leetcode WC-108-03 931-下降路径最小和

2018.10.28 12:15 给定一个方形整数数组 A,我们想要得到通过 A 的下降路径的最小和. 下降路径可以从第一行中的任何元素开始,并从每一行中选择一个元素.在下一行选择的元素和当前行所选元素最多相隔一列. 示例: 输入:[[1,2,3],[4,5,6],[7,8,9]] 输出:12 解释: 可能的下降路径有: [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],

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

【LeetCode】triangle求最小和路径

今天闲来无事,做了三道leetcode水题,怒更两发博客,也挺不错.今天更新的两篇都是dp解法的简单题. 题目要求如下. Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7],

Leetcode:Minimum Path Sum 最小路径和

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. 此题解法同Triangle 数字三角形.在此不再赘述. class

[LeetCode] Minimum Height Trees 最小高度树

For a undirected graph with tree characteristics, we can choose any node as the root. The result graph is then a rooted tree. Among all possible rooted trees, those with minimum height are called minimum height trees (MHTs). Given such a graph, write