【LeetCode】解题84:Largest Rectangle in Histogram

本博客详细介绍了如何解决LeetCode第84题——Largest Rectangle in Histogram,包括两种解题方法:分治法和栈。通过分治法,时间复杂度为O(nlogn);栈的解决方案则利用空间换取时间,遍历过程中维护柱子高度递增,以计算最大面积。最终给出Java实现方案。
摘要由CSDN通过智能技术生成

Problem 84: Largest Rectangle in Histogram [Hard]

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.

Example:

Input: [2,1,5,6,2,3]
Output: 10

来源:LeetCode

解题思路

1. 分治法

在一组不同高度的柱子中求取最大面积,可以通过将其中的最矮柱子作为分割点进行分治,公式为:
m a x a r e a ( 0 , N − 1 ) = max ⁡ { m a x a r e a ( 0 , i n d e x ∗ − 1 ) , N ∗ h e i g h t s [ i n d e x ∗ ] , m a x a r e a ( i n d e x ∗ + 1 , N − 1 ) } max_{area}(0, N-1) = \max\{max_{area}(0, index^*-1), N * heights[index^*], max_{area}(index^*+1, N-1) \} maxarea(0,N1)=max{ maxarea(0,index

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值