当前位置:网站首页>[simple] 155 Minimum stack
[simple] 155 Minimum stack
2022-06-12 21:52:00 【Caicai 2022】
subject
Code
Execution time :64 ms, In all Python3 Defeated in submission 41.59% Users of
Memory consumption :18.2 MB, In all Python3 Defeated in submission 43.94% Users of
Pass the test case :31 / 31
class MinStack:
def __init__(self):
self.A,self.B=[],[]
def push(self, val: int) -> None:
self.A.append(val)
if not self.B or val<=self.B[-1]:
self.B.append(val)
def pop(self) -> None:
if self.A.pop()==self.B[-1]:
self.B.pop()
def top(self) -> int:
return self.A[-1]
def getMin(self) -> int:
return self.B[-1]
【 Method 2】
Execution time :60 ms, In all Python3 Defeated in submission 54.96% Users of
Memory consumption :18.9 MB, In all Python3 Defeated in submission 5.10% Users of
Pass the test case :31 / 31
class MinStack:
def __init__(self):
self.A=[]
def push(self, val: int) -> None:
if not self.A:
self.A.append((val,val))
else:
self.A.append((val,min(val,self.A[-1][1])))
def pop(self) -> None:
self.A.pop()
def top(self) -> int:
return self.A[-1][0]
def getMin(self) -> int:
return self.A[-1][1]
【 Method 3】 Push forward
No additional auxiliary stack space
Execution time :60 ms, In all Python3 Defeated in submission 54.96% Users of
Memory consumption :18.4 MB, In all Python3 Defeated in submission 26.99% Users of
Pass the test case :31 / 31
class MinStack:
def __init__(self):
""" initialize your data structure here. """
self.stack = []
self.min_value = -1
def push(self, x: int) -> None:
if not self.stack:
self.stack.append(0)
self.min_value = x
else:
diff = x-self.min_value
self.stack.append(diff)
self.min_value = self.min_value if diff > 0 else x
def pop(self) -> None:
if self.stack:
diff = self.stack.pop()
if diff < 0:
top = self.min_value
self.min_value = top - diff
else:
top = self.min_value + diff
return top
def top(self) -> int:
return self.min_value if self.stack[-1] < 0 else self.stack[-1] + self.min_value
def getMin(self) -> int:
return self.min_value if self.stack else -1
边栏推荐
- 回文链表及链表相交问题(和心怡的人相交)你真的会了吗?
- How to write a vscode plug-in by yourself to realize plug-in freedom!
- 2021 rust survey results released: 9354 questionnaires collected
- 六月集训(第11天) —— 矩阵
- Jin AI her power | impact tech, she can
- SQL调优指南笔记12:Configuring Options for Optimizer Statistics Gathering
- OceanBase 社区版 OCP 功能解读
- Okio source code analysis
- [QNX hypervisor 2.2 user manual] 4.3 obtain the host component
- Kdd2022 | graphmae: self supervised mask map self encoder
猜你喜欢
SQL tuning guide notes 16:managing historical optimizer statistics
SQL调优指南笔记15:Controlling the Use of Optimizer Statistics
Palindrome linked list and linked list intersection problem (intersecting with Xinyi people) do you really know?
Npoi create word
KDD2022 | GraphMAE:自监督掩码图自编码器
Graphics2d class basic use
SQL tuning guide notes 18:analyzing statistics using optimizer statistics Advisor
Pixel level reconstruction and restoration technology to solve severe image blur
MySQL体系结构及基础管理(二)
SQL调优指南笔记16:Managing Historical Optimizer Statistics
随机推荐
logstash时间戳转换为unix 纳秒nano second time
ICML2022 | GALAXY:極化圖主動學習
Ansible PlayBook et ansible roles (3)
How to implement a simple publish subscribe mode
SQL tuning guide notes 9:joins
SQL调优指南笔记14:Managing Extended Statistics
Jdbctemplate inserts and returns the primary key
Zip压缩解压缩
【QNX Hypervisor 2.2 用户手册】4.3 获取host组件
The ifeq, filter and strip of makefile are easy to use
回文链表及链表相交问题(和心怡的人相交)你真的会了吗?
Smart management of green agriculture: a visual platform for agricultural product scheduling
Simple understanding of cap and base theory
SQL tuning guide notes 10:optimizer statistics concepts
Oracle livelabs experiment: introduction to Oracle Spatial
SQL tuning guide notes 14:managing extended statistics
Unity 常用3D数学计算
Oracle 19c 安装文档
lambda表达式与流优化代码
[QNX hypervisor 2.2 user manual] 4.2 supported build environments