[Leetcode] 11 Container With Most Water

Source: Internet
Author: User

[Leetcode] 11 Container With Most Water

First of all, when I first saw this question, I thought about it in the direction of dynamic planning. Later, I could not create a transfer equation. So I want to take the maximum value in three ways:

1) right shifts one digit to the left.

2) shifts one digit to the right of Left.

3) Right shifts one digit to the left, and left shifts one digit to the Right.

The three values take the maximum value, but it cannot be proved that local optimization can bring about global optimization, and the inverse example is found. We imagine that if the left and right are close to one place for the sake of local optimization, we may miss a maximum height. So how can we get better than the maximum height?

You only need to move the lower side to the center each time, and record the maximum area during iteration. This ensures that we do not miss the height. Every time we move low, we expect a higher height. It is already relatively high. It must be searched unless it becomes low. Otherwise, it must be maintained.

 

Class Solution {public: int maxArea (vector

 
  
& Height) {int left = 0, right = height. size ()-1; int ans = 0, temp; while (left

  
   
Height [right]) right --; else left ++;} return ans ;}};

  

 


 

 

 

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.