Container With Most Water

Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). 

n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). 
Find two lines, which together with x-axis forms a container, such that the container contains the most water.
Note: You may not slant the container.

Solution: From both sides to the center.

 1 class Solution {
 2 public:
 3     int maxArea(vector<int> &height) {
 4         int res = 0;
 5         int l = 0, r = height.size()-1;
 6         while (l < r) {
 7             if (height[l] <= height[r]) {
 8                 res = max(res, (r-l) * height[l]);
 9                 l++;
10             }
11             else {
12                 res = max(res, (r-l) * height[r]);
13                 r--;
14             }
15         }
16         return res;
17     }
18 };
原文地址:https://www.cnblogs.com/zhengjiankang/p/3646793.html