Solution with O(n), 16ms, C++

  • 0

    Firstly choose the first one and the last one as the front bound and the rear bound. For the next step, the height of the front bound and the rear bound should be higher than the min(height[front],height[rear]).

    class Solution {
        int maxArea(vector<int>& height) {
            if(height.size()<2)return 0;
            int Area = 0,front  = 0,rear = height.size()-1;
            while(front < rear){
                Area = max(Area,min(height[front],height[rear])*(rear-front));
                int h = min(height[front],height[rear]);
                while(front<rear && height[front]<=h) front++;
                while(front<rear && height[rear]<=h) rear--;
            return Area;

Log in to reply

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