LeetCode 84 Largest Rectangle in Histogram (Python详解及实现)
2017-08-09 15:34
666 查看
【题目】
Given n non-negative integers representingthe histogram's bar height where the width of each bar is 1, find the area oflargest rectangle in the histogram.
Above is a histogram where width of eachbar is 1, given height = [2,1,5,6,2,3].
The largest rectangle is shown in theshaded area, which has area = 10 unit.
For example,
Given heights = [2,1,5,6,2,3],
return 10.
【思路】
用栈来模拟,遍历heights数组,并比较与栈顶元素stack[-1]的大小:
l 大于栈顶元素,就push进去;
l 等于栈顶元素,则忽略
l 小于栈顶元素,持续弹栈,并记录这个高度的最大面积,直至栈为空。然后将弹出的数全部替换为降序点的值,即做到了整体依然是有序非降的。
整个过程中,即把所有的局部最大矩阵计算过了,又在宽度范围内保留了全部的场景。
宽度=当前索引-前索引
例,2,1,5,6,3的模拟过程。
先加入一个0,方便最后可以全部弹栈出来。变成:2,1,5,6,2,3.
u 2进栈,stack = [2],maxArea = 0;
u 1比栈顶元素stack[-1]小,2出栈,maxArea = 2*1 =2;2被替换为1进栈,1继续进栈,stack = [1,1] ,maxArea =2;
u 5比栈顶元素stack[-1]大,5进栈,栈为stack = [1,1,5] ,maxArea =2;
u 6比栈顶元素stack[-1]大,6进栈,栈为stack = [1,1,5,6] ,maxArea =2;
u 2比栈顶元素stack[-1]小,是一个降序点;此时栈长为4,开始弹栈,6出栈,maxArea =6*1=6(当前height*1(4-数字6在栈中的下标));接着判断,2比栈顶元素5小,5出栈maxArea =5*2=6(当前height = 5,width =4-2=2(4-数字5在栈中的下标));下一个1比,2小,不需出栈。然后将弹出5、6的空位压栈为2,2继续入栈,stack = [1,1,2,2,2],maxArea = 10;
u 3比栈顶元素2大,入栈,stack = [1,1,2,2,2,3],maxArea = 10;
u 最后判断每个点作为起始点的最大面积,max(height[i]*(size-i))=max{3*1, 2*2, 2*3, 2*4, 1*5, 1*6}= 8 < 10。遍历结束。整个过程中max的area为10.
【Python实现】
方法一:暴力算法,全部遍历(TLE)
class Solution:
""":type heights: List[int]
:rtype: int
"""
def largestRectangleArea(self, height):
maxArea=0
for i in range(len(height)):
min = height[i]
for j in range(i, len(height)):
if height[j] < min:
min = height[j]
if min*(j-i+1) > maxArea:
maxArea = min*(j-i+1)
return maxArea
if __name__ == '__main__':
S= Solution()
heights = [2,1,5,6,2,3]
maxarea = S.largestRectangleArea(heights)
print(maxarea)
方法二:栈模拟
class Solution:
""":type heights: List[int]
:rtype: int
"""
def largestRectangleArea(self, heights):
maxArea = 0
stack = []
i = 0
while i < len(heights):
if len(stack) == 0 or stack[-1] <= heights[i]:
stack.append(heights[i])
else:
count = 0
while len(stack) > 0 andstack[-1] > heights[i]:#height[i]小于栈顶元素
count += 1
maxArea = max(maxArea,stack[-1]*count)
stack.pop()#栈顶元素出栈
while count > 0:#将当前height入栈
count -= 1
stack.append(heights[i])
stack.append(heights[i])
i += 1
count = 1
while len(stack) != 0:
maxArea = max(maxArea, stack[-1]*count)
stack.pop()
count += 1
return maxArea
if __name__ == '__main__':
S= Solution()
heights = [2,1,5,6,2,3]
maxarea = S.largestRectangleArea(heights)
print(maxarea)
Given n non-negative integers representingthe histogram's bar height where the width of each bar is 1, find the area oflargest rectangle in the histogram.
Above is a histogram where width of eachbar is 1, given height = [2,1,5,6,2,3].
The largest rectangle is shown in theshaded area, which has area = 10 unit.
For example,
Given heights = [2,1,5,6,2,3],
return 10.
【思路】
用栈来模拟,遍历heights数组,并比较与栈顶元素stack[-1]的大小:
l 大于栈顶元素,就push进去;
l 等于栈顶元素,则忽略
l 小于栈顶元素,持续弹栈,并记录这个高度的最大面积,直至栈为空。然后将弹出的数全部替换为降序点的值,即做到了整体依然是有序非降的。
整个过程中,即把所有的局部最大矩阵计算过了,又在宽度范围内保留了全部的场景。
宽度=当前索引-前索引
例,2,1,5,6,3的模拟过程。
先加入一个0,方便最后可以全部弹栈出来。变成:2,1,5,6,2,3.
u 2进栈,stack = [2],maxArea = 0;
u 1比栈顶元素stack[-1]小,2出栈,maxArea = 2*1 =2;2被替换为1进栈,1继续进栈,stack = [1,1] ,maxArea =2;
u 5比栈顶元素stack[-1]大,5进栈,栈为stack = [1,1,5] ,maxArea =2;
u 6比栈顶元素stack[-1]大,6进栈,栈为stack = [1,1,5,6] ,maxArea =2;
u 2比栈顶元素stack[-1]小,是一个降序点;此时栈长为4,开始弹栈,6出栈,maxArea =6*1=6(当前height*1(4-数字6在栈中的下标));接着判断,2比栈顶元素5小,5出栈maxArea =5*2=6(当前height = 5,width =4-2=2(4-数字5在栈中的下标));下一个1比,2小,不需出栈。然后将弹出5、6的空位压栈为2,2继续入栈,stack = [1,1,2,2,2],maxArea = 10;
u 3比栈顶元素2大,入栈,stack = [1,1,2,2,2,3],maxArea = 10;
u 最后判断每个点作为起始点的最大面积,max(height[i]*(size-i))=max{3*1, 2*2, 2*3, 2*4, 1*5, 1*6}= 8 < 10。遍历结束。整个过程中max的area为10.
【Python实现】
方法一:暴力算法,全部遍历(TLE)
class Solution:
""":type heights: List[int]
:rtype: int
"""
def largestRectangleArea(self, height):
maxArea=0
for i in range(len(height)):
min = height[i]
for j in range(i, len(height)):
if height[j] < min:
min = height[j]
if min*(j-i+1) > maxArea:
maxArea = min*(j-i+1)
return maxArea
if __name__ == '__main__':
S= Solution()
heights = [2,1,5,6,2,3]
maxarea = S.largestRectangleArea(heights)
print(maxarea)
方法二:栈模拟
class Solution:
""":type heights: List[int]
:rtype: int
"""
def largestRectangleArea(self, heights):
maxArea = 0
stack = []
i = 0
while i < len(heights):
if len(stack) == 0 or stack[-1] <= heights[i]:
stack.append(heights[i])
else:
count = 0
while len(stack) > 0 andstack[-1] > heights[i]:#height[i]小于栈顶元素
count += 1
maxArea = max(maxArea,stack[-1]*count)
stack.pop()#栈顶元素出栈
while count > 0:#将当前height入栈
count -= 1
stack.append(heights[i])
stack.append(heights[i])
i += 1
count = 1
while len(stack) != 0:
maxArea = max(maxArea, stack[-1]*count)
stack.pop()
count += 1
return maxArea
if __name__ == '__main__':
S= Solution()
heights = [2,1,5,6,2,3]
maxarea = S.largestRectangleArea(heights)
print(maxarea)
相关文章推荐
- LeetCode 70 Climbing Stairs(Python详解及实现)
- LeetCode 60 Permutation Sequence (Python实现及详解)
- LeetCode 77 Combinations (Python详解及实现)
- LeetCode 75 SortColor(Python详解及实现)
- LeetCode 81 Search in Rotated Sorted Array II (Python详解及实现)
- LeetCode 71 Simplify Path(Python详解及实现)
- LeetCode 65 ValidNumer(Python详解及实现)
- LeetCode 87 Scramble String (Python详解及实现)
- LeetCode 92 Reverse Linked List II (Python详解及实现)
- LeetCode 79 Word Search (Python详解及实现)
- LeetCode 93 Restore IP Addresses(Python详解及实现)
- LeetCode 55 Jump Game(Python 实现及详解)
- LeetCode 89 Gray Code (Python详解及实现)
- 25_LeetCode 82&83. Remove Duplicates from Sorted List(II) (Python详解及实现)
- LeetCode 74 Search a 2D Matrix(Python详解及实现)
- LeetCode 90 SubsetsII (Python详解及实现)
- LeetCode 66 Plus One (Python详解及实现)
- LeetCode 85 Maximal Rectangle (Python详解及实现)
- LeetCode 68 Text Justification(Python详解及实现)
- LeetCode 76 Minimum Window Substring(Python详解及实现)