Home > AI > Data Structure > Stack >

(HackerRank) Largest Rectangle

https://www.hackerrank.com/challenges/largest-rectangle/problem

Solution (Java) Brute Force.

But this solution cannot pass all tests for time limit exceeded error.

// Complete the largestRectangle function below.
    static long largestRectangle(int[] h) {
        int maxArea = 0;
        for (int i=0; i<h.length; i++){
            int minHeight = h[i];
            for (int j=i; j<h.length; j++) {
                minHeight = Math.min(minHeight, h[j]);
                int currentArea = minHeight * (j-i+1);
                maxArea = Math.max(maxArea, currentArea);
            }
        }
        return maxArea;
    }

Leave a Reply