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