O(N^2),but faster than most O(N) algorithms for this test cases :)


  • 0
    S

    class Solution {
        public int maxArea(int[] height) {
            int max = 0, lm = 0, rm = 0;
            for(int i = 0; i < height.length - 1; ++i) {
                if(height[i] < lm) continue;
                lm = height[i];
                rm = 0;
                for(int j = height.length - 1; j > i; --j) {
                    if(height[j] < rm) continue;
                    rm = height[j];
                    int size = (j - i) * (height[i] < height[j] ? height[i] : height[j]);
                    if(size > max)
                        max = size;
                    if(rm > lm) break;
                }
            }
            return max;
        }
    }
    
    Beats 88.98 :)

Log in to reply
 

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