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;
    }
}
相关推荐
qmx_0722 分钟前
HTB-Jerry(tomcat war文件、msfvenom)
java·web安全·网络安全·tomcat
为风而战30 分钟前
IIS+Ngnix+Tomcat 部署网站 用IIS实现反向代理
java·tomcat
limingade1 小时前
手机实时提取SIM卡打电话的信令和声音-新的篇章(一、可行的方案探讨)
物联网·算法·智能手机·数据分析·信息与通信
技术无疆2 小时前
快速开发与维护:探索 AndroidAnnotations
android·java·android studio·android-studio·androidx·代码注入
jiao000014 小时前
数据结构——队列
c语言·数据结构·算法
迷迭所归处5 小时前
C++ —— 关于vector
开发语言·c++·算法
架构文摘JGWZ5 小时前
Java 23 的12 个新特性!!
java·开发语言·学习
leon6255 小时前
优化算法(一)—遗传算法(Genetic Algorithm)附MATLAB程序
开发语言·算法·matlab
CV工程师小林5 小时前
【算法】BFS 系列之边权为 1 的最短路问题
数据结构·c++·算法·leetcode·宽度优先
Navigator_Z6 小时前
数据结构C //线性表(链表)ADT结构及相关函数
c语言·数据结构·算法·链表