【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));
    }
}
相关推荐
一切皆是因缘际会18 小时前
AI Agent落地困局与突破:从技术架构到企业解析
数据结构·人工智能·算法·架构
sheeta199818 小时前
LeetCode 每日一题笔记 日期:2026.05.16 题目:154. 寻找旋转排序数组中的最小值 II
笔记·算法·leetcode
计算机安禾18 小时前
【c++面向对象编程】第28篇:new/delete vs malloc/free:C++中正确动态内存管理
开发语言·c++·算法
qeen8718 小时前
【算法笔记】各种常见排序算法详细解析(下)
c语言·数据结构·c++·笔记·学习·算法·排序算法
欢璃18 小时前
笔试强训练习
java·开发语言·jvm·数据结构·算法·贪心算法·动态规划
400分18 小时前
# LangChain v0.2+ 与 Ollama 三大核心模型实战指南
算法
fish_xk19 小时前
c++11的初见
开发语言·c++·算法
搬砖者(视觉算法工程师)19 小时前
计算机视觉与计算摄影测量学第三讲图像直方图:理论、统计特性与点运算变换
人工智能·算法·计算机视觉
Yingjun Mo19 小时前
3. Meta-Harness:模型基座外壳的端到端优化
人工智能·算法
Cthy_hy19 小时前
并查集(Disjoint Set Union):巧判「连通聚类关系」的极简利器
数据结构·算法