[Leetcode] 120 Triangle

Source: Internet
Author: User

[Leetcode] 120 Triangle

After acm is done, this question is too basic ....

There is nothing to say. The simplest dynamic planning is to find the state transfer equation. The bottom-up approach is used (in this way, only dp [0] [0] is the answer). The results of each node in the current layer are calculated based on the accumulated subgrade of the next row, take either the left or the right, and take the smallest of the two.
State transition equation: triangle [I] [j] + = min (triangle [I + 1] [j], triangle [I + 1] [j + 1])

 

Class Solution {public: int minimumTotal (vector

 
  
> & Triangle) {for (int I = triangle. size ()-2; I> = 0; -- I) for (int j = 0; j <= I; ++ j) {triangle [I] [j] + = min (triangle [I + 1] [j + 1], triangle [I + 1] [j]);} return triangle [0] [0] ;}};

 
Similar questions include the hdu 2084 data Tower, which is consistent with the solution in this question.

 

More advanced: hdu 1176 free pie

 

Train of Thought: we can regard all the time periods and pies as a matrix, where time is the number of rows, and the number of columns is the number triangle. We can find a path from the bottom layer, make the sum on the path largest. The state transition equation is dp [I] [j] = max (dp [I + 1] [J-1], dp [I + 1] [j], dp [I + 1] [J-1]) + pie [I] [j]. Pie [I] [j] is the number of pies dropped at the position j at the time I.

The specific code will not be posted here. You should try your best to implement it.



 

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.