Leetcode: Triangle

Source: Internet
Author: User

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 is11(I. e., 2 + 3 + 5 + 1 = 11 ).

Note:
Bonus Point if you are able to do this using onlyO(N) Extra space, whereNIs the total number of rows in the triangle.

Address: https://oj.leetcode.com/problems/triangle/

Algorithm: open two n-sized arrays. Suppose we have completed the search for the first line, the minimum path of the End Node with the J element of row I is stored in an array. Assume that the array points to the destination node with the pointer pre. Next, we need to complete the search on line I + 1, the path with the J element of line I + 1 as the final node must be from the J node of line I or the J + 1 node of line I, when calculating the minimum path, you only need to consider these two paths and store the results in another array. This array points to the pointer p. Because only the last result is needed in each search process, we only use two arrays and use the pre and P pointers to reuse these two arrays, in this way, the space requirements of O (n) can be met. Code:

 1 class Solution { 2 public: 3     int minimumTotal(vector<vector<int> > &triangle) { 4         int n = triangle.size(); 5         if(n < 1)    return 0; 6         if(n == 1)   return triangle[0][0]; 7         vector<int> min1(n); 8         vector<int> min2(n); 9         min1[0] = triangle[0][0];10         vector<int> *pre = &min1;11         vector<int> *p   = &min2;12         for(int i = 1; i < n; ++i){13             for(int j = 0; j <= i; ++j){14                 if(j == 0){15                     (*p)[j] = (*pre)[j] + triangle[i][j];16                 }else if(j == i){17                     (*p)[j] = (*pre)[j-1] + triangle[i][j];18                 }else{19                     (*p)[j] = ( (*pre)[j] > (*pre)[j-1] ? (*pre)[j-1] : (*pre)[j] ) + triangle[i][j];20                 }21             }22             vector<int> *t = pre;23             pre = p;24             p = t;25         }26         int result = (*pre)[0];27         for(int i = 1; i < n; ++i){28             if (result > (*pre)[i]) result = (*pre)[i];29         }30         return result;31     }32     33 };

 

Leetcode: Triangle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.