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.

这个需要证明贪心算法的正确性

class Solution {
public:
    int maxArea(vector<int> &height) {
        int len = height.size();
        int left = 0 ; 
        int right  = len-1;
        int max = 0 ;
        while(left<right)
        {
            int temp = (right - left) * (height[left] < height[right] ? height[left] :height[right]);
            max = max > temp ? max :temp;
            if(height[left] > height[right])
            right--;
            else
            left ++;
        }
        return max;
    }
};

  

原文地址:https://www.cnblogs.com/pengyu2003/p/3666649.html