-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathqueueWithMin.py
49 lines (35 loc) · 1.08 KB
/
queueWithMin.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
''' Design a queue that supports push_rear, pop_front, and get_min in O(1).
Would that be elegantly possible too? '''
import collections
class minQueue:
data = collections.deque()
minCandidates = collections.deque() # save the possible mins
def isEmpty(self):
return len(self.data) == 0
def dequeue(self):
if self.isEmpty():
return None
if self.minCandidates[0] == self.data[0]:
self.minCandidates.popleft()
return self.data.popleft()
def enqueue(self, e):
self.data.append(e)
while len(self.minCandidates) > 0 and self.minCandidates[0] >= e:
self.minCandidates.popleft()
self.minCandidates.append(e)
def min(self):
if self.isEmpty():
return None
return self.minCandidates[0]
if __name__ == '__main__':
import random
data = [random.randint(1, 100) for x in range(15)]
print(data)
q = minQueue()
for i in data:
q.enqueue(i)
print(q.min())
print
for i in data:
q.dequeue()
print(q.min())