Java for LeetCode 084 Largest Rectangle in Histogram【HARD】

For example,
Given height = [2,1,5,6,2,3],
return 10.

解题思路:

参考Problem H: Largest Rectangle in a Histogram第四种思路,或者翻译版Largest Rectangle in [email protected],JAVA实现如下:

    public int largestRectangleArea(int[] height) {
        Stack<Integer> stk = new Stack<Integer>();
        int ret = 0;
        for (int i = 0; i <= height.length; i++) {
        	int h=0;
        	if(i<height.length)
        		h=height[i];
            if (stk.isEmpty() || h >= height[stk.peek()])
                stk.push(i);
            else {
            	int top = stk.pop();
                ret = Math.max(ret, height[top] * (stk.empty() ? i : i - stk.peek() - 1));
                i--;
            }
        }
        return ret;
}
时间: 2024-12-23 03:41:54

Java for LeetCode 084 Largest Rectangle in Histogram【HARD】的相关文章

[LeetCode OJ] Largest Rectangle in Histogram

Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3]. The largest

C++]LeetCode: 133 Largest Rectangle in Histogram(最大矩形面积)

题目: Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3]. The larg

leetcode 84 Largest Rectangle in Histogram ----- java

Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3]. The largest

LeetCode 84 Largest Rectangle in Histogram (单调栈)

Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3]. The largest

LeetCode之Largest Rectangle in Histogram浅析

首先上题目 Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. For example,Given height = [2,1,5,6,2,3],return 10. 题目的大意即给出一个柱状图的歌数据,求柱状图内所包含的最大矩形. 这道

[leetcode]84.Largest Rectangle in Histogram ,O(n)解法剖析

Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3]. The largest

leetCode 84.Largest Rectangle in Histogram (最大矩形直方图) 解题思路和方法

Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3]. The largest

19.2.22 [LeetCode 84] Largest Rectangle in Histogram

Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3]. The largest

084 Largest Rectangle In Histogram

这道题有两个做法 总体来说都是O(n) 但是第二个方法是第一个的简化版 方法一: l[i] = max(j) 其中a[j], a[j+1], ...,a[i-1] < a[i] r[i] = min(j) 其中 a[i] > a[i+1], ... ,a[j] dif[i] = r[i] - l[i] area[i] = a[i] * dif[i] 最大的area[i] 即为答案 a[i] : -inf, 2, 3, 1, 4, 6, 5, 7, 3, 2, -inf i     :    0