您的位置:首页 > 其它

Leetcode_84 Largest Rectangle in Histogram

2017-11-30 10:17 387 查看
        今天看了看Leetcode_84题,难度是hard,趁着空闲,简单地写一写吧。

原题链接:https://leetcode.com/problems/largest-rectangle-in-histogram/?tab=Description

原题题目:

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 heights = 
[2,1,5,6,2,3]
,

return 
10
.

        这道题开始我没有想出比较好的解法,因此参考了网上的解法,网上利用堆栈的方式解决了这道题,仔细想想本解法还是很有道理的。利用堆栈来实现类似的升序排列,时间复杂度为o(n^2)。

        程序如下图所示:





        最终的运行时间截图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: