C++
Java
Python
Python3
C
C#
JavaScript
Ruby
Swift
Go
Scala
Kotlin
Rust
PHP
TypeScript
Racket
Erlang
Elixir
Dart
monokai
ambiance
chaos
chrome
cloud9_day
cloud9_night
cloud9_night_low_color
clouds
clouds_midnight
cobalt
crimson_editor
dawn
dracula
dreamweaver
eclipse
github
github_dark
gob
gruvbox
gruvbox_dark_hard
gruvbox_light_hard
idle_fingers
iplastic
katzenmilch
kr_theme
kuroir
merbivore
merbivore_soft
mono_industrial
nord_dark
one_dark
pastel_on_dark
solarized_dark
solarized_light
sqlserver
terminal
textmate
tomorrow
tomorrow_night
tomorrow_night_blue
tomorrow_night_bright
tomorrow_night_eighties
twilight
vibrant_ink
xcode
上次编辑到这里,代码来自缓存 点击恢复默认模板
class MaxQueue {
public:
MaxQueue() {
}
int max_value() {
}
void push_back(int value) {
}
int pop_front() {
}
};
/**
* Your MaxQueue object will be instantiated and called as such:
* MaxQueue* obj = new MaxQueue();
* int param_1 = obj->max_value();
* obj->push_back(value);
* int param_3 = obj->pop_front();
*/
运行代码
提交
python3 解法, 执行用时: 284 ms, 内存消耗: 18.8 MB, 提交时间: 2022-11-14 10:27:10
import queue
class MaxQueue:
def __init__(self):
self.deque = queue.deque() # 保存排序过的队列
self.queue = queue.Queue()
def max_value(self) -> int:
return self.deque[0] if self.deque else -1
def push_back(self, value: int) -> None:
while self.deque and self.deque[-1] < value: # 只保留>=value的元素
self.deque.pop()
self.deque.append(value)
self.queue.put(value)
def pop_front(self) -> int:
if not self.deque:
return -1
ans = self.queue.get()
if ans == self.deque[0]:
self.deque.popleft()
return ans
# Your MaxQueue object will be instantiated and called as such:
# obj = MaxQueue()
# param_1 = obj.max_value()
# obj.push_back(value)
# param_3 = obj.pop_front()
python3 解法, 执行用时: 176 ms, 内存消耗: 19 MB, 提交时间: 2022-11-14 10:14:29
import queue
class MaxQueue:
def __init__(self):
self.deque = queue.deque()
def max_value(self) -> int:
return max(self.deque) if self.deque else -1
def push_back(self, value: int) -> None:
self.deque.append(value)
def pop_front(self) -> int:
return self.deque.popleft() if self.deque else -1
# Your MaxQueue object will be instantiated and called as such:
# obj = MaxQueue()
# param_1 = obj.max_value()
# obj.push_back(value)
# param_3 = obj.pop_front()
python3 解法, 执行用时: 188 ms, 内存消耗: 19 MB, 提交时间: 2022-11-14 10:13:57
class MaxQueue:
def __init__(self):
self.arr = []
def max_value(self) -> int:
if len(self.arr) == 0: return -1
return max(self.arr)
def push_back(self, value: int) -> None:
self.arr.append(value)
def pop_front(self) -> int:
if len(self.arr) == 0: return -1
num = self.arr[0]
self.arr = self.arr[1:]
return num
# Your MaxQueue object will be instantiated and called as such:
# obj = MaxQueue()
# param_1 = obj.max_value()
# obj.push_back(value)
# param_3 = obj.pop_front()