本文主要是介绍Java | Leetcode Java题解之第84题柱状图中最大的矩形,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目:
题解:
class Solution {public int largestRectangleArea(int[] heights) {int n = heights.length;int[] left = new int[n];int[] right = new int[n];Arrays.fill(right, n);Deque<Integer> mono_stack = new ArrayDeque<Integer>();for (int i = 0; i < n; ++i) {while (!mono_stack.isEmpty() && heights[mono_stack.peek()] >= heights[i]) {right[mono_stack.peek()] = i;mono_stack.pop();}left[i] = (mono_stack.isEmpty() ? -1 : mono_stack.peek());mono_stack.push(i);}int ans = 0;for (int i = 0; i < n; ++i) {ans = Math.max(ans, (right[i] - left[i] - 1) * heights[i]);}return ans;}
}
这篇关于Java | Leetcode Java题解之第84题柱状图中最大的矩形的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!