Easy to understand, Java, O(n), beats 98.49%


  • 0
    X
    public class Solution {
        public int maxArea(int[] height) {
            int p=0, q=height.length-1;
            int max=0, h=0;
            while(p<q){
                h = Math.min(height[p],height[q]);
                max = Math.max(max, h*(q-p));
                
                while(p<height.length && h>=height[p])    p++;
                while(q>=0 && h>=height[q])  q--;
            }
            return max;
        }
    }
    

Log in to reply
 

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