leetcode84柱状图中最大的矩形

题解: - 力扣(LeetCode)

java 复制代码
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;
    }
}
相关推荐
2501_9032386534 分钟前
自定义登录页面的Spring Security实践
java·后端·spring·个人开发
JNU freshman37 分钟前
力扣第435场周赛讲解
算法·leetcode·蓝桥杯
眼镜哥(with glasses)38 分钟前
蓝桥杯python基础算法(2-2)——基础算法(B)——模拟(上)
算法
飞翔的佩奇1 小时前
Java项目: 基于SpringBoot+mybatis+maven+mysql实现的图书管理系统(含源码+数据库+答辩PPT+毕业论文)
java·数据库·spring boot·mysql·spring·毕业设计·图书管理
赵鑫亿2 小时前
7.DP算法
算法·dp
iqay2 小时前
【C语言】填空题/程序填空题1
c语言·开发语言·数据结构·c++·算法·c#
还有糕手3 小时前
算法【有依赖的背包】
算法·动态规划
jerry6093 小时前
注解(Annotation)
java·数据库·sql
Future_yzx3 小时前
Java Web的发展史与SpringMVC入门学习(SpringMVC框架入门案例)
java·前端·学习
pursuit_csdn3 小时前
力扣 347. 前 K 个高频元素
算法·leetcode