0
点赞
收藏
分享

微信扫一扫

LeetCode 84. 柱状图中最大的矩形

皮皮球场 2022-05-01 阅读 181

LeetCode 84. 柱状图中最大的矩形

文章目录

题目描述

LeetCode 84. 柱状图中最大的矩形
提示:

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

一、解题关键词


二、解题报告

1.思路分析

2.时间复杂度

3.代码示例

class Solution {
public int largestRectangleArea(int[] heights) {
        int len = heights.length;
        int res = 0;
        int[] left = new int[len];
        int[] right = new int[len];
        Arrays.fill(right, len);

        Deque<Integer> stack = new ArrayDeque<Integer>();
        for (int i = 0; i < len; i++) {
            while (!stack.isEmpty() && heights[stack.peek()] > heights[i]) {
                right[stack.peek()] = i;
                stack.pop();
            }
            left[i] = (stack.isEmpty()) ? -1 : stack.peek();
            stack.push(i);
        }
        for (int i = 0; i < len; i++) {
            res = Math.max(res, (right[i] - left[i] - 1) * heights[i]);
        }

        return res;

    }
}

2.知识点



总结

相同题目

xxx

举报

相关推荐

0 条评论