A fast C++ solution


  • 0
    S
    class Solution {
    public:
        int maxArea(vector<int>& height) {
            if (height.size() <= 1) return 0;
            int low = 0, high = height.size() - 1;
            int area = min(height[low], height[high]) * (high - low);
            while (low < high) {
                if (height[low] < height[high]) {
                    int lowH = height[low++];
                    while (height[low] <= lowH && low < high) {
                        low++;
                    }
                }
                else {
                    int highH = height[high--];
                    while (height[high] <= highH && low < high) {
                        high--;
                    }
                }
                area = max(area, min(height[low], height[high]) * (high - low));
            }
            return area;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.