Stack method solved
This commit is contained in:
parent
b9b739b93d
commit
78b7c57b40
@ -4,37 +4,27 @@ class Solution(object):
|
||||
:type heights: List[int]
|
||||
:rtype: int
|
||||
"""
|
||||
rect = []
|
||||
h = len(heights)
|
||||
heights.append(0)
|
||||
stack = [-1]
|
||||
ans = 0
|
||||
for i in range(len(heights)):
|
||||
while heights[i] < heights[stack[-1]]:
|
||||
h = heights[stack.pop()]
|
||||
w = i - stack[-1] - 1
|
||||
ans = max(ans, h * w)
|
||||
stack.append(i)
|
||||
heights.pop()
|
||||
return ans
|
||||
|
||||
if len(heights) <= 2:
|
||||
if len(heights) == 1:
|
||||
return heights[0]
|
||||
else:
|
||||
if heights[0] == 0 or heights[1] == 0:
|
||||
return max(heights[0], heights[1])
|
||||
low = min(heights[0], heights[1])
|
||||
return low*2
|
||||
|
||||
for x in range(h):
|
||||
count = 1
|
||||
for y in range(x+1, h):
|
||||
if heights[x] == min(heights):
|
||||
rect.append(heights[x]*len(heights))
|
||||
elif heights[x] > heights[y]:
|
||||
# print(f'{heights[x]} * {count} = {heights[x]*count}')
|
||||
rect.append(heights[x]*count)
|
||||
else:
|
||||
count += 1
|
||||
|
||||
if rect:
|
||||
return max(rect)
|
||||
else:
|
||||
return 0
|
||||
obj = Solution()
|
||||
# print(obj.largestRectangleArea([2,1,5,6,2,3])) # 10
|
||||
# print(obj.largestRectangleArea([2,4])) # 4
|
||||
# print(obj.largestRectangleArea([0,9])) # 9
|
||||
# print(obj.largestRectangleArea([0,0,0])) # 0
|
||||
# print(obj.largestRectangleArea([2,1,2])) # 3
|
||||
print(obj.largestRectangleArea([1,2,2]) # 4
|
||||
# print(obj.largestRectangleArea([1,2,2])) # 4
|
||||
# print(obj.largestRectangleArea([0,2,0])) # 2
|
||||
# print(obj.largestRectangleArea([5,4,1,2])) # 8
|
||||
# print(obj.largestRectangleArea([2,0,2])) # 2
|
||||
# print(obj.largestRectangleArea([1,2,3,4,5])) # 9
|
||||
# print(obj.largestRectangleArea([4,2,0,3,2,5])) # 6
|
||||
|
||||
@ -1,4 +1,10 @@
|
||||
print(obj.largestRectangleArea([2,1,5,6,2,3])) # 10
|
||||
print(obj.largestRectangleArea([2,4])) # 4
|
||||
print(obj.largestRectangleArea([0,9])) # 9
|
||||
print(obj.largestRectangleArea([0,0,0])) # 9
|
||||
stack = [-1]
|
||||
ans = 0
|
||||
for i in xrange(len(height)):
|
||||
while height[i] < height[stack[-1]]:
|
||||
h = height[stack.pop()]
|
||||
w = i - stack[-1] - 1
|
||||
ans = max(ans, h * w)
|
||||
stack.append(i)
|
||||
height.pop()
|
||||
return ans
|
||||
Loading…
Reference in New Issue
Block a user