Easy one, but need code reviews to improve coding style.
My Code
#!/usr/bin/python
from collections import deque
class MaxQueue(object):
"""MaxQueue"""
def __init__(self, init=None):
self._queue = deque()
self._max_queue = deque()
if init:
for element in init:
self.push(element)
def push(self, element):
"""Insert an element into the queue"""
while self._max_queue and self._max_queue[-1] < element:
self._max_queue.pop()
self._max_queue.append(element)
self._queue.append(element)
def pop(self):
"""Pop out one element from queue"""
try:
element = self._queue.popleft()
except IndexError:
raise Exception("pop from empty maxqueue")
if element == self._max_queue[0]:
self._max_queue.popleft()
return element
def get_max(self):
"""Get max element of the queue"""
if self._max_queue:
return self._max_queue[0]
return None
Question_ID: Q002
Language: python3
Time Cost: 13-mins
Time Complexity: O(n)???
Solution
My Code
Other
How can i improve my solution? Any suggestions?