1. How I sloved
Heap had to be made into two stacks: ‘stackin’ and ‘stackout’. ‘stackin’ was used for push(), peek(), empty(), and pop(), and ‘stackout’ was used only for pop(). The new element was added at the end of the ‘stackin’ using the built-in function ‘append()’ in ‘push()’, the first element was pointed in ‘peek()’, and the length of ‘stackin’ was checked to be 0 or not in ‘empty()’. All elements were copied from ‘stackin’ to ‘stackout’, the built-in function ‘pop()’ was used in ‘stackout’. And the first element of ‘stackin’ was deleted.
2. Code
class MyQueue:
def __init__(self):
self.stackin = []
self.stackout = []
def push(self, x: int) -> None:
self.stackin.append(x)
def pop(self) -> int:
self.stackout = self.stackin[::-1]
self.stackin = self.stackin[1::]
return self.stackout.pop()
def peek(self) -> int:
return self.stackin[0]
def empty(self) -> bool:
return True if len(self.stackin)==0 else False
3. Result
Runtime : 20 ms(98.36%), Memory usage : 14.2 MB(9.87%)
(Runtime can be different by a system even if it is a same code.)