【Hot100】LeetCode—84. 柱状图中最大的矩形

目录

  • [1- 思路](#1- 思路)
  • [2- 实现](#2- 实现)
    • [⭐++84. 柱状图中最大的矩形++------题解思路](#⭐84. 柱状图中最大的矩形——题解思路)
  • [3- ACM 实现](#3- ACM 实现)


1- 思路

题目识别

  • 识别1 :给定一个数组 heights ,求解柱状图的最大面积

单调栈

  • 使用 Stack 来实现,遍历数组,存入下标。
  • 本题实现的是一个单调递减栈,目标是找一个以当前为基准,左边第一个比这个小的以及右边第一个比该元素小的元素。

2- 实现

⭐++84. 柱状图中最大的矩形++------题解思路

java 复制代码
class Solution {
    public int largestRectangleArea(int[] heights) {
        int len = heights.length;
        int[] newH = new int[len+2];

        int index = 1;
        for(int i = 0 ; i < len;i++){
            newH[index++] = heights[i];
        }

        heights = newH;
        len = heights.length;
        Stack<Integer> st = new Stack<>();
        int res = 0;
        st.push(0);
        // 单调栈
        for(int i = 0 ; i < len;i++){
            if(heights[i] >= heights[st.peek()]){
                st.push(i);
            }else{
                while(heights[i] < heights[st.peek()]){
                    int mid = st.peek();
                    st.pop();
                    int left = st.peek();
                    int right = i;
                    int h = heights[mid];
                    int width = right-left-1;
                    res = Math.max(res,h*width);
                }
                st.push(i);
            }
        }

        return res;
    }
}

3- ACM 实现

java 复制代码
public class maxArea {


    public static int maxA(int[] heights){
        int len = heights.length;
        int[] newH = new int[len+2];
        int index = 1;
        for(int i = 1 ; i < len;i++){
            newH[index++] = heights[i];
        }

        heights = newH;
        len = heights.length;
        Stack<Integer> st = new Stack<>();
        int res = 0;
        st.push(0);
        // 单调栈
        for(int i = 0 ; i < len;i++){
            if(heights[i] >= heights[st.peek()]){
                st.push(i);
            }else{
                while(heights[i] < heights[st.peek()]){
                    int mid = st.peek();
                    st.pop();
                    int left = st.peek();
                    int right = i;
                    int h = heights[mid];
                    int width = right-left-1;
                    res = Math.max(res,h*width);
                }
                st.push(i);
            }
        }

        return res;
    }


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();
        input = input.replace("[","").replace("]","");
        String[] parts = input.split(",");
        int[] nums = new int[parts.length];
        for(int i = 0 ; i < nums.length;i++){
            nums[i] = Integer.parseInt(parts[i]);
        }
        System.out.println("结果是"+maxA(nums));
    }
}
相关推荐
CoovallyAIHub2 小时前
中科大DSAI Lab团队多篇论文入选ICCV 2025,推动三维视觉与泛化感知技术突破
深度学习·算法·计算机视觉
NAGNIP3 小时前
Serverless 架构下的大模型框架落地实践
算法·架构
moonlifesudo3 小时前
半开区间和开区间的两个二分模版
算法
moonlifesudo3 小时前
300:最长递增子序列
算法
CoovallyAIHub8 小时前
港大&字节重磅发布DanceGRPO:突破视觉生成RLHF瓶颈,多项任务性能提升超180%!
深度学习·算法·计算机视觉
CoovallyAIHub8 小时前
英伟达ViPE重磅发布!解决3D感知难题,SLAM+深度学习完美融合(附带数据集下载地址)
深度学习·算法·计算机视觉
聚客AI1 天前
🙋‍♀️Transformer训练与推理全流程:从输入处理到输出生成
人工智能·算法·llm
大怪v1 天前
前端:人工智能?我也会啊!来个花活,😎😎😎“自动驾驶”整起!
前端·javascript·算法
惯导马工1 天前
【论文导读】ORB-SLAM3:An Accurate Open-Source Library for Visual, Visual-Inertial and
深度学习·算法
骑自行车的码农1 天前
【React用到的一些算法】游标和栈
算法·react.js