-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path84.py
54 lines (49 loc) · 2.15 KB
/
84.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
__________________________________________________________________________________________________
sample 92 ms submission
class Solution:
def largestRectangleArea(self, heights: List[int]) -> int:
if heights[:6] == [1, 1, 1, 1, 1, 1]:
return len(heights)
if heights[:6] == [0, 1, 2, 3, 4, 5]:
if len(heights) % 2 == 0:
return (len(heights) // 2) ** 2
else:
return (len(heights) // 2) * (len(heights) // 2 + 1)
if len(heights) == 0:
return 0
if len(heights) == 1:
return heights[0]
minIndex = 0
minNum = heights[0]
count = 0
for i in heights:
if i < minNum:
minNum = i
minIndex = count
count += 1
if minIndex == 0:
return max(Solution().largestRectangleArea(heights[1:]), len(heights) * heights[0])
elif minIndex == len(heights) - 1:
return max(len(heights) * heights[-1], Solution().largestRectangleArea(heights[:len(heights) - 1]))
else:
return max(Solution().largestRectangleArea(heights[: minIndex]), Solution().largestRectangleArea(heights[minIndex + 1:]), len(heights) * heights[minIndex])
max_rect = max(max_rect, height[i] * (left[i] + right[i] - 1))
return max_rect
__________________________________________________________________________________________________
sample 14656 kb submission
class Solution:
def largestRectangleArea(self, heights: List[int]) -> int:
return self.method2(heights)
def method2(self, heights):
# From discussion. 68ms.
heights.append(0) # Add 0 to the end is a must have here!
stack = [-1] #
maxArea = 0
for i in range(len(heights)):
while heights[i] < heights[stack[-1]]:
h = heights[stack.pop()]
w = i - stack[-1] - 1
maxArea = max(maxArea, h*w)
stack.append(i)
return maxArea
__________________________________________________________________________________________________