【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));
    }
}
相关推荐
jingfeng5142 分钟前
C++ STL-string类底层实现
前端·c++·算法
雲墨款哥1 小时前
JS算法练习-Day10-判断单调数列
前端·javascript·算法
FPGA1 小时前
CRC校验原理及其FPGA实现
算法
Jina AI1 小时前
回归C++: 在GGUF上构建高效的向量模型
人工智能·算法·机器学习·数据挖掘·回归
Coovally AI模型快速验证1 小时前
YOLO、DarkNet和深度学习如何让自动驾驶看得清?
深度学习·算法·yolo·cnn·自动驾驶·transformer·无人机
luoqice1 小时前
linux下找到指定目录下最新日期log文件
linux·算法
楽码2 小时前
底层技术SwissTable的实现对比
数据结构·后端·算法
fffcccc11122 小时前
初级背包问题,层层剖析为什么这样做。最好需要自己推演一遍。
算法
瓦特what?3 小时前
关于C++的#include的超超超详细讲解
java·开发语言·数据结构·c++·算法·信息可视化·数据挖掘
楽码3 小时前
自动修复GoVet:语言实现对比
后端·算法·编程语言