码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode]Largest Rectangle in Histgram,解题报告

时间:2015-04-23 13:33:11      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:leetcode

目录


题目

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 rectangle is shown in the shaded area, which has area = 10 unit.

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


思路一

我们使用最简单的两层for循环方法。对于每一个柱行图i的高度h,我们把i的宽度分别往左和右延展,求出每个点住状图左右延展后的最大矩形面积,然后取最大值。

左右延展的限制条件是:

i >= 0 && height[i] >= h
i < height.length && height[i] >= h

代码如下:

public class Solution {

    public int largestRectangleArea(int[] height) {

        if (height == null || height.length == 0) return 0;

        int area = 0;

        for (int i = 0; i < height.length; i++) {

            int high = height[i];

            int width = 1;

            for (int j = i - 1; j >= 0 && height[j] >= high; j--) {

                width++;

            }

            for (int j = i + 1; j < height.length && height[j] >= high; j++) {

                width++;

            }

            area = Math.max(area, high * width);

        }

        return area;

    }

}

不过,有个问题就是,这种做法在LeetCode上是没法通过大集合测试的,因为该算法的时间复杂度是O(n^2)。接下来,我们看一下,如何将O(n^2)的时间复杂度降低为O(n)。


思路二

这里介绍一种使用栈的O(n)解法。想要降低时间复杂度,必然相应的要增加空间复杂度了。

我们用栈存储height数组高度递增的下标。因为,对于每一个柱形图的高度来说,分为两种情况:

  1. 当栈为空或者当前高度大于栈顶下标对应的高度时,则当前下标入栈。
  2. 不为1的情况,则当前栈顶下标出栈,计算当前栈顶下标所代表高度的最大矩形。既然,高度已经已经确定,那宽度又分为两种情况(假设遍历点的下标为i):

    • 当栈为空时,宽度为当前遍历点i的大小。(左边界为0,因为如果左边有比当前出栈下标高度更高的点,则栈不可能为空。右边界为i - 1, 因为i下标对应的高度是小于当前出栈下标高度的。所以最终的宽度为:[i - 1 - 0 + 1] = [i])。
    • 当栈不为空时,宽度为i - stack.peek() - 1。(左边界为stack.peek()的下标 + 1,因为当前stack.peek()的高度小于h。右边界为当前坐标i - 1。所以最终的宽度为:[i - 1 - (stack.peek() + 1) + 1] = [i - stack.peek() - 1])。

AC代码

public class Solution {

    public int largestRectangleArea(int[] height) {

        if (height == null || height.length == 0) return 0;

        int area = 0, len = height.length;

        LinkedList<Integer> stack = new LinkedList<Integer>();

        for (int i = 0; i < len; i ++) {

            if (stack.isEmpty() || height[stack.peek()] <= height[i]) {

                stack.push(i);

            } else {

                int index = stack.pop();

                int high = height[index];

                int width = stack.isEmpty() ? i : i - stack.peek() - 1; 

                area = Math.max(area, high * width);

                i -= 1;

            }

        }

        while (!stack.isEmpty()) {

            int index = stack.pop();

            int high = height[index];

            int width = stack.isEmpty() ? len : len - stack.peek() - 1; 

            area = Math.max(area, high * width);

        }

        return area;

    }
}

[LeetCode]Largest Rectangle in Histgram,解题报告

标签:leetcode

原文地址:http://blog.csdn.net/wzy_1988/article/details/45218823

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!