【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));
    }
}
相关推荐
枫景Maple31 分钟前
LeetCode 2297. 跳跃游戏 VIII(中等)
算法·leetcode
鑫鑫向栄32 分钟前
[蓝桥杯]修改数组
数据结构·c++·算法·蓝桥杯·动态规划
鑫鑫向栄33 分钟前
[蓝桥杯]带分数
数据结构·c++·算法·职场和发展·蓝桥杯
小wanga1 小时前
【递归、搜索与回溯】专题三 穷举vs暴搜vs回溯vs剪枝
c++·算法·机器学习·剪枝
天宫风子1 小时前
线性代数小述(一)
线性代数·算法·矩阵·抽象代数
hjyowl2 小时前
题解:AT_abc407_c [ABC407C] Security 2
c语言·开发语言·算法
ai产品老杨3 小时前
减少交通拥堵、提高效率、改善交通安全的智慧交通开源了。
前端·vue.js·算法·ecmascript·音视频
小于不是小鱼呀3 小时前
手撕 K-Means
人工智能·算法·机器学习
m0_740154673 小时前
K-Means颜色变卦和渐变色
算法·机器学习·kmeans