Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
max-stack.py 1.78 KB
一键复制 编辑 原始数据 按行查看 历史
Allen Liu 提交于 2018-12-30 21:04 +08:00 . remove semicolons
# Time: push: O(1)
# pop: O(n), there is no built-in SortedDict in python. If applied, it could be reduced to O(logn)
# popMax: O(n)
# top: O(1)
# peekMax: O(1)
# Space: O(n), n is the number of values in the current stack
import collections
class MaxStack(object):
def __init__(self):
"""
initialize your data structure here.
"""
self.__idx_to_val = collections.defaultdict(int)
self.__val_to_idxs = collections.defaultdict(list)
self.__top = None
self.__max = None
def push(self, x):
"""
:type x: int
:rtype: void
"""
idx = self.__val_to_idxs[self.__top][-1]+1 if self.__val_to_idxs else 0
self.__idx_to_val[idx] = x
self.__val_to_idxs[x].append(idx)
self.__top = x
self.__max = max(self.__max, x)
def pop(self):
"""
:rtype: int
"""
val = self.__top
self.__remove(val)
return val
def top(self):
"""
:rtype: int
"""
return self.__top
def peekMax(self):
"""
:rtype: int
"""
return self.__max
def popMax(self):
"""
:rtype: int
"""
val = self.__max
self.__remove(val)
return val
def __remove(self, val):
idx = self.__val_to_idxs[val][-1]
self.__val_to_idxs[val].pop()
if not self.__val_to_idxs[val]:
del self.__val_to_idxs[val]
del self.__idx_to_val[idx]
if val == self.__top:
self.__top = self.__idx_to_val[max(self.__idx_to_val.keys())] if self.__idx_to_val else None
if val == self.__max:
self.__max = max(self.__val_to_idxs.keys()) if self.__val_to_idxs else None
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助