Container with most water

Source: Internet
Author: User

The thought of this question is simple:

From both sides, the short board goes in the middle. I can think about it carefully. Why can this guarantee the maximum capacity? First, it must be okay from both sides, because it includes all the boards. Why is the short board right in the middle? The short board limits the container capacity. If the long board goes in the middle, the resulting container must be smaller than the previous container (the width is reduced, and the height cannot be increased ). While the short board goes inside, although the width is reduced, it may increase the height to get a larger container.

Int maxarea (vector <int> & height) {auto left = height. Begin (); Auto right = height. End ()-1; int result = 0; while (left! = Right) {int width = distance (left, right); If (* left> * right) {result = max (result, width * (* right )); right --;} else {result = max (result, width * (* left); left ++;} return result ;}

Container with most water

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.