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;
    }
}
相关推荐
LuXi_foryou几秒前
Cannot resolve symbol ‘view‘ Androidstudio报错解决办法
android·java·android studio
仟濹35 分钟前
【前缀和与差分 二分搜索 C/C++】洛谷 P1083 借教室
c语言·c++·算法
架构文摘JGWZ41 分钟前
不用 Tomcat?SpringBoot 项目用啥代替?
java·spring boot·tomcat
xinxiangwangzhi_1 小时前
多视图几何--从线性变换到射影变换--2线性变换
人工智能·算法·计算机视觉
AI技术控1 小时前
计算机视觉算法实战——昆虫识别检测(主页有源码)
人工智能·算法·计算机视觉
网安墨雨1 小时前
网络安全之命令
java·运维·web安全
sd21315122 小时前
springboot3 spring security+jwt实现接口权限验证实现
java·后端·spring
张国荣家的弟弟2 小时前
【Yonghong 企业日常问题07 】 东方通TongWeb替代Tomcat的实战指南!
java·tomcat
局外人_Jia2 小时前
Tomcat 新手入门指南
java·tomcat