You are viewing a single comment's thread. Return to all comments →
Java O(n)
public static long largestRectangle(List<Integer> h) { Stack<Integer> stack = new Stack<>(); long maxArea = 0; int n = h.size(); for (int i = 0; i < n; i++) { while (!stack.isEmpty() && h.get(stack.peek()) >= h.get(i)) { int height = h.get(stack.pop()); int width = stack.isEmpty() ? i : i - stack.peek() - 1; maxArea = Math.max(maxArea, (long) height * width); } stack.push(i); } while (!stack.isEmpty()) { int height = h.get(stack.pop()); int width = stack.isEmpty() ? n : n - stack.peek() - 1; maxArea = Math.max(maxArea, (long) height * width); } return maxArea; }
Seems like cookies are disabled on this browser, please enable them to open this website
Largest Rectangle
You are viewing a single comment's thread. Return to all comments →
Java O(n)