[LeetCode]120.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.


【分析】

这是一道动态规划的题目,求一个三角形二维数组从顶到低端的最小路径和。

我们从低端向顶端计算。设状态为 S[i][j]表示从从位置 ( i, j ) 出发,到最低端路径的最小和

状态转移方程:
S[i][j] = min(S[i+1][j] + S[i+1][j+1]) +S[i][j]

S[0][0]就是要求解的答案。

时间复杂度 O(n^2) ,空间复杂度 O(1)

【代码】

    /**------------------------------------
    *   日期:2015-02-03
    *   作者:SJF0115
    *   题目: 120.Triangle
    *   网址:https://oj.leetcode.com/problems/triangle/
    *   结果:AC
    *   来源:LeetCode
    *   博客:
    ---------------------------------------**/
    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;

    class Solution {
    public:
        int minimumTotal(vector<vector<int> > &triangle) {
            int size = triangle.size();
            // down-to-top
            // 第i层
            for(int i = size - 2;i >= 0;--i){
                // 第i层的第j个元素
                for(int j = 0;j <= i;++j){
                    triangle[i][j] += min(triangle[i+1][j],triangle[i+1][j+1]);
                }//for
            }//for
            return triangle[0][0];
        }
    };

    int main(){
        Solution s;
        vector<vector<int> > triangle = {{2},{3,4},{6,5,3},{4,1,8,3}};
        int result = s.minimumTotal(triangle);
        // 输出
        cout<<result<<endl;
        return 0;
    }



【思路二】

一位网友说:虽然是O(1)的空间复杂度,但其实破坏了三角形本身啊。如果算上占用的三角形本身的空间,实际使用的空间复杂度应该算O(N^2)了。

只求值不求本身的时候,都是可以用空间轮转这个办法的。如LCS、走棋盘等等一批问题,都可以这么节省。

从上面思路的状态转移方程中看出:S[i][j] = min(S[i+1][j] + S[i+1][j+1]) +S[i][j]

S[i][j]只与下一行的第j个元素和第j+1个元素相关,i的关系是固定的,因此我们可以省去这一维。

开辟O(N)的数组,然后规划的时候使用S[j] = min(S[j+1], S[j) +Triangle[i][j]就可以了。

【代码二】

    /*------------------------------------
    *   日期:2015-02-03
    *   作者:SJF0115
    *   题目: 120.Triangle
    *   网址:https://oj.leetcode.com/problems/triangle/
    *   结果:AC
    *   来源:LeetCode
    *   博客:
    ---------------------------------------*/
    class Solution {
    public:
        int minimumTotal(vector<vector<int> > &triangle) {
            int size = triangle.size();
            vector<int> next(triangle.back());
            // down-to-top
            // 第i层
            for(int i = size - 2;i >= 0;--i){
                // 第i层的第j个元素
                for(int j = 0;j <= i;++j){
                    next[j] = min(next[j],next[j+1]) + triangle[i][j];
                }//for
            }//for
            return next[0];
        }
    };


目录
相关文章
|
Java Go
POJ 1163 The Triangle
POJ 1163 The Triangle
84 0
Triangle Leetcode
Created by Wang, Jerry, last modified on Dec 20, 2015
92 0
Triangle Leetcode
LeetCode 118:杨辉三角 II Pascal's Triangle II
公众号:爱写bug(ID:icodebugs)作者:爱写bug 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行。 Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle. Note that the row index starts from 0. 在杨辉三角中,每个数是它左上方和右上方的数的和。
886 0
Leetcode 118:Pascal's Triangle 杨辉三角
118:Pascal's Triangle 杨辉三角 Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。
955 0
|
算法
[LeetCode]--223. Rectangle Area
Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined by its bottom left corner and top right corner as shown in the figure. Rectangle Area
1194 0
poj-1163-The Triangle
Description 73 88 1 02 7 4 44 5 2 6 5(Figure 1) Figure 1 shows a number triangle.
668 0