【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));
    }
}
相关推荐
爱coding的橙子2 小时前
每日算法刷题 Day3 5.11:leetcode数组2道题,用时1h(有点慢)
算法·leetcode
Dream it possible!5 小时前
LeetCode 热题 100_只出现一次的数字(96_136_简单_C++)(哈希表;哈希集合;排序+遍历;位运算)
c++·leetcode·位运算·哈希表·哈希集合
?abc!7 小时前
缓存(5):常见 缓存数据淘汰算法/缓存清空策略
java·算法·缓存
BioRunYiXue7 小时前
一文了解氨基酸的分类、代谢和应用
人工智能·深度学习·算法·机器学习·分类·数据挖掘·代谢组学
jiunian_cn7 小时前
【c++】异常详解
java·开发语言·数据结构·c++·算法·visual studio
工藤新一¹9 小时前
蓝桥杯算法题 -蛇形矩阵(方向向量)
c++·算法·矩阵·蓝桥杯·方向向量
Levin__NLP_CV_AIGC9 小时前
解决pip安装PyPI默认源速度慢
算法·pip
Helibo449 小时前
GESPC++六级复习
java·数据结构·算法
EnticE15210 小时前
[高阶数据结构]二叉树经典面试题
数据结构·算法·面试
MarkHard12310 小时前
Leetcode (力扣)做题记录 hot100(34,215,912,121)
算法·leetcode·职场和发展