Leetcode first _ Triangle

Source: Internet
Author: User

A very simple DP, we can see that the space limit is O (N). Do not habitually think that it is to save the minimum value of each row, it is easy to think of the sum of the numbers in a row when they are the end points of the path. When the last row is reached, it is the shortest path and at the bottom position as the end point from the top to the bottom. You can find the smallest one.

You should pay attention to this problem when implementing it. Because we need to use the data of the previous row During computation, we should calculate it from the back to avoid data overwriting. This technique is used a lot, I believe everyone is familiar with it.

The ac code is as follows:

Class Solution {public: int minimumTotal (vector

 
  
> & Triangle) {int lines = triangle. size (); if (lines = 0) return 0; vector

  
   
Res (lines, 0); for (int I = 0; I

   
    
0) res [I] = res [I-1] + triangle [I] [I]; for (int j = I-1; j> 0; j --) {res [j] = min (res [J-1], res [j]) + triangle [I] [j];} res [0] = res [0] + triangle [I] [0];} int mmin = res [0]; for (int I = 1; I

    

     

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.