LeetCode 84 柱状图中最大的矩形

题目描述

柱状图中最大的矩形

给定 n 个非负整数,用来表示柱状图中各个柱子的高度。每个柱子彼此相邻,且宽度为 1 。

求在该柱状图中,能够勾勒出来的矩形的最大面积。

示例 1:

输入:heights = [2,1,5,6,2,3]
输出:10
解释:最大的矩形为图中红色区域,面积为 10

示例 2:

输入: heights = [2,4]
输出: 4

提示:

  • 1 <= heights.length <=105
  • 0 <= heights[i] <= 104

解法

解法:暴力解法

java代码:

java 复制代码
class Solution {
    public int largestRectangleArea(int[] heights) {
        if (heights.length == 0) {
            return 0;
        }

        int res = 0;
        for (int i = 0; i < heights.length; i++) {
            if (heights[i] == 0) {
                continue;
            }
            // 找左边大于等于当前值的位置,注意先比较前一个位置的值,再++,防止越界
            int l = i;
            while (l > 0 && heights[l - 1] >= heights[i]) {
                l --;
            }

            // 找右边大于等于当前值的位置,注意先比较后一个位置的值,再++,防止越界
            int r = i;
            while (r < heights.length - 1 && heights[r + 1] >= heights[i]) {
                r ++;
            }

            // 计算当前位置高度的最大矩形面积
            int area = (r - l + 1) * heights[i];
            res = Math.max(area, res);
        }

        return res;
    }
}

复杂度

  • 时间复杂度:O(N^2) N是输入数组的长度
  • 空间复杂度:O(1)

解法2:单调栈

java

java 复制代码
class Solution {
    public int largestRectangleArea(int[] heights) {
        int len = heights.length;
        if (len == 0) {
            return 0;
        }
        if (len == 1) {
            return heights[0];
        }

        int res = 0;
        Deque<Integer> stack = new ArrayDeque<>();

        // 第一遍循环,入栈,并把能计算出来的先计算了(就是当前元素小于栈顶元素了,栈顶元素是可以计算的了)
        for (int i = 0; i < len; i++) {
            // 这个 while 很关键,因为有可能不止一个柱形的最大宽度可以被计算出来
            while (!stack.isEmpty() && heights[i] < heights[stack.peekLast()]) {
                int curHeight = heights[stack.pollLast()];
                while (!stack.isEmpty() && heights[stack.peekLast()] == curHeight) {
                    stack.pollLast();
                }

                int curWidth;
                if (stack.isEmpty()) {
                    curWidth = i;
                } else {
                    curWidth = i - stack.peekLast() - 1;
                }
                res = Math.max(res, curHeight * curWidth);
            }
            stack.addLast(i);
        }

        // 第二遍,栈不为空,继续弹栈
        while (!stack.isEmpty()) {
            int curHeight = heights[stack.pollLast()];
            while (!stack.isEmpty() && heights[stack.peekLast()] == curHeight) {
                stack.pollLast();
            }
            int curWidth;
            if (stack.isEmpty()) {
                curWidth = len;
            } else {
                curWidth = len - stack.peekLast() - 1;
            }
            res = Math.max(res, curHeight * curWidth);
        }

        return res;
    }
}
相关推荐
EPSDA14 分钟前
Java的IO流(二)
java·开发语言
小灰灰爱代码16 分钟前
C++——将数组a[5]={-1,2,9,-5,7}中小于0的元素置成0。并将其结果输出(要求:用数组名作为函数的参数来实现)
数据结构·c++·算法
zzlyyds1 小时前
SpringBoot---------Actuator监控
java·spring boot·spring·actuator
vitobo1 小时前
Java的JDBC编程
java·开发语言
呆萌小新@渊洁1 小时前
后端接收数组,集合类数据
android·java·开发语言
Mopes__2 小时前
Python | Leetcode Python题解之第421题数组中两个数的最大异或值
python·leetcode·题解
A_cot2 小时前
深入了解 Maven 和 Redis
java·redis·maven
liuyang-neu2 小时前
力扣中等 33.搜索旋转排序数组
java·数据结构·算法·leetcode
爱吃烤鸡翅的酸菜鱼2 小时前
java(3)数组的定义与使用
java·开发语言·idea·intellij idea
ganjiee00072 小时前
力扣(leetcode)每日一题 2414 最长的字母序连续子字符串的长度
java·算法·leetcode