-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLargestRectangle.py
61 lines (53 loc) · 1.23 KB
/
LargestRectangle.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
55
56
57
58
59
60
61
x = [1,2,3,4,5,2,1,2]
y = [11,11,10,10,10]
z = [1,3,2,1,2]
f = [9,4,6,5,7,3,5,7,2,1]
m = [6320,6020,6098,1332,7263,672,9472,2838,3401,9494]
class Stack:
def __init__(self):
self.stack = []
def listConvert(self,l):
self.stack = l
def getLength(self):
return len(self.stack)
def push(self,n):
self.stack.append(n)
def pop(self):
temp = self.top()
self.stack.pop()
return temp
def top(self):
return self.stack[len(self.stack)-1]
def isEmpty(self):
if self.stack == []:
return True
else:
return False
def clean(self):
self.stack = []
def code(l):
position = Stack()
max_score = 0
i=0
while i < len(l):
if position.isEmpty() == True or l[position.top()] <= l[i]:
position.push(i)
i+=1
else:
temp = position.pop()
if position.isEmpty() == True:
area = l[temp] * i
else:
area = l[temp] * (i-position.top()-1)
if max_score < area:
max_score = area
while position.isEmpty() == False:
temp = position.pop()
if position.isEmpty() == True:
area = l[temp] * i
else:
area = l[temp] * (i-position.top()-1)
if max_score < area :
max_score = area
return max_score
print(code(m))