-
Notifications
You must be signed in to change notification settings - Fork 0
/
monotonous_stack.py
43 lines (31 loc) · 918 Bytes
/
monotonous_stack.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
'''
A monotonous stack is just like a stack, except all elements in it is
monotonously increasing or decreasing
Created on May 6, 2019
@author: tonytan4ever
'''
class MonotonousStack:
def __init__(self, has_pop_handler=False):
self.stack = []
self.has_pop_handler = has_pop_handler
def push(self, target_val):
while self.size > 0 and self.top >= target_val:
val = self.pop()
if self.has_pop_handler:
self.pop_handler(val, target_val)
self.stack.append(target_val)
def pop(self):
return self.stack.pop()
@property
def size(self):
return len(self.stack)
@property
def top(self):
if self.size > 0:
return self.stack[-1]
else:
raise ValueError
def pop_handler(self, val, target_val):
raise NotImplemented
if __name__ == '__main__':
pass