[C++]LeetCode: 81 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 bottom is 11 (i.e., 2 + 3 + 5 + 1 =
11).

Note:

Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.

错误思路:

根据题意,每次选择只考虑相邻的数字,直接维护一个位置变量和sum变量。忘记考虑三角形可能有重复数字(本次选择最小,不一定会导致全局最小),贪心算法在此处不适用。需要用动态规划。

Error Code:

<span style="font-size:14px;">class Solution {
public:
    int minimumTotal(vector<vector<int> > &triangle) {
        if(triangle.size() == 0 || triangle[0].size() == 0) return 0;
        int sum = triangle[0][0];
        int position = 0;

        for(int i = 1; i < triangle.size(); i++)
        {
            if(triangle[i][position] < triangle[i][position+1])
            {
                sum += triangle[i][position];
            }
            else
            {
                sum += triangle[i][position+1];
                position += 1;
            }
        }

        return sum;
    }
};</span>

Submission Result: Wrong
Answer
More Details

Input: [[-1],[2,3],[1,-1,-3]]
Output: 0
Expected: -1

贪心路径:-1 -->2-->-1; 正确路径:-1-->3-->-3

正确解法:动态规划

思路:由于top-down的动态规划,空间上有冗余,每次都需要重复保存结果(如第一行为方便第二行计算,需要保存两个值),而且需要特殊处理头尾的元素。所以考虑采用bottom-up的动态规划,从最后一层开始往上计算,设dp[i][j]表示以第i层j个元素为起点的最小路径和,动态规划方程为:

dp[i][j] = value[i][j] + min(dp[i-1][j], dp[i-1][j+1])   (根据题目中的例子,题意中相邻元素指正下(i)和正下右边一个元素(i+1),不包括正下左边的元素(i-1))

自底向上的计算,每个dp值保存了这个节点的实际最小路径和。

因为每一层之和之和它的下一层的值有关,所以我们只需要一个一维数组保存下层的值

For
the
kth

level:minpath[i]
= min(
minpath[i],
minpath[i+1])
+ triangle[k][i];

注意这里,由于我们是min(minpath[i], minpath[i+1]),和之前处理的动态规划不同,[C++]LeetCode:
56 Unique Paths
. 这里两个值minpath[i], minpath[i+1]都是没有更新的,即表示的就是dp[i-1][j],
dp[i-1][j+1]的含义,都是下一层的值。

Attention:

1.
初始化时,我们用最后一行的值。

//bottom-up 用最后一行元素初始化dp
        vector<int> dp(triangle.back());

2. 循环时,我们从倒数第二行开始,并且,每行的列的值也就只能取到行坐标的值(最大),如,倒数第二行最后一列坐标为n-2. 注意循环条件。

 for(int i = row-2; i >= 0; i--)
        {
            for(int j = 0; j <= i; j++)
            {
                dp[j] = triangle[i][j] + min(dp[j], dp[j+1]);
            }
        }

3.
最后返回dp[0]的值,即为top的最后的最短路径。

复杂度:时间复杂度O(N^2),
空间O(N)

AC
Code:

class Solution {
public:
    int minimumTotal(vector<vector<int> > &triangle) {
        int row = triangle.size();
        if(row == 0 || triangle[0].size() == 0) return 0;

        //bottom-up 用最后一行元素初始化dp
        vector<int> dp(triangle.back());

        for(int i = row-2; i >= 0; i--)
        {
            for(int j = 0; j <= i; j++)
            {
                dp[j] = triangle[i][j] + min(dp[j], dp[j+1]);
            }
        }

        return dp[0];
    }
};

时间: 2024-11-02 02:23:54

[C++]LeetCode: 81 Triangle的相关文章

LeetCode: 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 bottom is 11 (i

【LeetCode】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 bottom is 11 (i

[LeetCode][JavaScript]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 bottom

【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] Valid Triangle Number 合法的三角形个数

Given an array consists of non-negative integers, your task is to count the number of triplets chosen from the array that can make triangles if we take them as side lengths of a triangle. Example 1: Input: [2,2,3,4] Output: 3 Explanation: Valid combi

[leetcode 120]triangle 空间O(n)算法

1 题目 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 bottom is 

LeetCode 120. Triangle 20170706 部分之前做了没写的题目

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 bottom is 11 (i

[leetcode] 120 Triangle

感觉acm做过之后,这种题太基本了.... 没啥好说的,最简单的动态规划,找出状态转移方程就可以了.采用由下到上的思想(这样最后只需要取出dp[0][0]就是答案),本层每个结点的结果根据下面一行的路基累计和而计算,要么取左边的,要么取右边的,两者取最小的即可. 状态转移方程:triangle[i][j] += min(triangle[i + 1][j], triangle[i + 1][j + 1]) class Solution { public: int minimumTotal(vec

leetCode(47):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 bottom is 11 (i