本文主要是介绍leetcode84柱状图中最大的矩形,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题解: - 力扣(LeetCode)
class Solution {public int largestRectangleArea(int[] heights) {Stack<Integer> stack = new Stack<>();int maxArea = Integer.MIN_VALUE;for(int i = 0;i < heights.length;i++){int curHeight = heights[i];while(!stack.empty() && curHeight < heights[stack.peek()]){int index = stack.pop();int left = (stack.empty()) ? 0 : stack.peek()+1;int tempArea = heights[index] * (i - left);maxArea = Math.max(maxArea,tempArea);}stack.push(i); }while(!stack.empty()){int index = stack.pop();int left = (stack.empty()) ? 0 : stack.peek()+1;int tempArea = heights[index] * (heights.length - left);maxArea = Math.max(maxArea,tempArea);}return maxArea;}
}
这篇关于leetcode84柱状图中最大的矩形的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!