11. Container With Most Water(装最多的水 双指针)

Given n non-negative integers a1a2, ..., an, where each represents a point at coordinate (iai). n vertical lines are drawn such that the two endpoints of line i is at (iai) 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 and n is at least 2.

 

 

l = 0 

r = n-1

a[l] <a[r]

l++

那边矮扔掉哪边

 

 

 1 class Solution {
 2 public:
 3     int maxArea(vector<int>& height) {
 4         int low = 0;
 5         int high = height.size() -1;
 6         int res = 0;
 7         while(low < high) {
 8             int cur_res = min(height[high],height[low]) * (high-low);
 9             res = max(res,cur_res);
10             if (height[low] < height[high]) low++;
11             else if (height[low] > height[high]) high--;
12             else if (height[low] == height[high]) low++;// high--  is the same
13         }
14         return res;
15     }
16 };

 

 

posted @ 2018-04-02 11:15  乐乐章  阅读(337)  评论(1编辑  收藏  举报