当前位置:网站首页>LeetCode 0155. 最小栈
LeetCode 0155. 最小栈
2022-08-03 23:26:00 【Tisfy】
【LetMeFly】155.最小栈
力扣题目链接:https://leetcode.cn/problems/min-stack/
设计一个支持 push
,pop
,top
操作,并能在常数时间内检索到最小元素的栈。
实现 MinStack
类:
MinStack()
初始化堆栈对象。void push(int val)
将元素val推入堆栈。void pop()
删除堆栈顶部的元素。int top()
获取堆栈顶部的元素。int getMin()
获取堆栈中的最小元素。
示例 1:
输入: ["MinStack","push","push","push","getMin","pop","top","getMin"] [[],[-2],[0],[-3],[],[],[],[]] 输出: [null,null,null,null,-3,null,0,-2] 解释: MinStack minStack = new MinStack(); minStack.push(-2); minStack.push(0); minStack.push(-3); minStack.getMin(); --> 返回 -3. minStack.pop(); minStack.top(); --> 返回 0. minStack.getMin(); --> 返回 -2.
提示:
-231 <= val <= 231 - 1
pop
、top
和getMin
操作总是在 非空栈 上调用push
,pop
,top
, andgetMin
最多被调用3 * 104
次
方法一:map
入栈时,真正入栈的同时,用哈希表将入栈的数字累加。
例如,C++中map<int, int>
默认是有序存放的,因此map<int, int>.begin()->first
就是栈中元素的最小值
出栈时,真正出栈的同时,哈希表中该出栈元素的个数减 1 1 1。如果减一之后出现次数为 0 0 0,就删除掉哈希表中这一键值对。
- 时间复杂度 O ( n log n ) O(n\log n) O(nlogn),其中 n n n是操作次数。这种方法严格上不能被称为“常数时间内”。
- 空间复杂度 O ( n ) O(n) O(n)
AC代码
C++
class MinStack {
private:
map<int, int> ma;
stack<int> st;
public:
MinStack() {
}
void push(int val) {
st.push(val);
ma[val]++;
}
void pop() {
int val = st.top();
st.pop();
ma[val]--;
if (!ma[val]) {
ma.erase(val);
}
}
int top() {
return st.top();
}
int getMin() {
return ma.begin()->first;
}
};
方法二:辅助栈
出栈的顺序是由入栈决定的。
我们可以额外开辟一个“辅助栈”,每次有元素入栈后,辅助栈中入栈当前栈中的最小元素( m i n { 辅助栈 . t o p ( ) , t h i s V a l } min\{辅助栈.top(), thisVal\} min{ 辅助栈.top(),thisVal})
例如当前栈中元素为-1 -2 3
(最小元素为 − 2 -2 −2),现在如果新入栈一个元素 6 6 6,那么你最小元素还是 − 2 -2 −2,就将 − 2 -2 −2压入辅助栈;如果现在入栈一个元素 − 8 -8 −8,那么最小元素就应该为 − 8 -8 −8,就往辅助栈中压入 − 8 -8 −8
原始栈:
| | | |
| -1 | | -2 |
| -2 | | -2 |
| 3 | | 3 |
+----+ +----+
栈 辅助栈
- 如果入栈 6 6 6:
| 6 | | -2 | | -1 | | -2 | | -2 | | -2 | | 3 | | 3 | +----+ +----+ 栈 辅助栈
- 如果入栈 − 8 -8 −8:
| -8 | | -8 | | -1 | | -2 | | -2 | | -2 | | 3 | | 3 | +----+ +----+ 栈 辅助栈
为了方便,可以在初始化时往辅助栈中放入元素INT_MAX
以防止辅助栈.top()越界
- 时间复杂度 O ( n ) O(n) O(n),其中 n n n是操作次数
- 空间复杂度 O ( n ) O(n) O(n)
AC代码
C++
class MinStack {
private:
stack<int> realStack;
stack<int> minStack;
public:
MinStack() {
minStack.push(INT_MAX);
}
void push(int val) {
realStack.push(val);
minStack.push(min(val, minStack.top()));
}
void pop() {
realStack.pop();
minStack.pop();
}
int top() {
return realStack.top();
}
int getMin() {
return minStack.top();
}
};
同步发文于CSDN,原创不易,转载请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/126144246
边栏推荐
- HCIP BGP实验报告
- 689. 三个无重叠子数组的最大和
- JS获得URL超链接的参数值
- override学习(父类和子类)
- The salary of soft testers at each stage, come to Kangkang, how much can you get?
- Zilliz 2023 Fall Campus Recruitment Officially Launched!
- 获国际权威认可 | 云扩科技入选《RPA全球市场格局报告,Q3 2022》
- Analysys Analysis: The transaction scale of China's online retail B2C market in Q2 2022 will reach 2,344.47 billion yuan
- leetcode/子串中不能有重复字符的最长子串
- 【论文阅读】TRO 2021: Fail-Safe Motion Planning for Online Verification of Autonomous Vehicles Using Conve
猜你喜欢
直播系统聊天技术(八):vivo直播系统中IM消息模块的架构实践
Creo 9.0二维草图的诊断:加亮开放端点
Fluorescein-PEG-CLS, cholesterol-polyethylene glycol-fluorescein scientific research reagent
Creo 9.0在草图环境中创建坐标系
软测人每个阶段的薪资待遇,快来康康你能拿多少?
Walk the Maze BFS
(PC+WAP)织梦模板螺钉手柄类网站
Interpretation of ML: A case of global interpretation/local interpretation of EBC model interpretability based on titanic titanic rescued binary prediction data set using interpret
跨域的学习
Why Flutter Flutter of tutorials is the best choice for business?
随机推荐
MiniAPI of .NET6 (14): Cross-domain CORS (Part 1)
1067 Sort with Swap(0, i)
获国际权威认可 | 云扩科技入选《RPA全球市场格局报告,Q3 2022》
数据分析知识点搜集(纯粹的搜集)
软件测试内卷严重,如何提升自己的竞争力呢?
代码随想录笔记_动态规划_416分割等和子集
智能座舱的「交互设计」大战
跨域的学习
【并发编程】ReentrantLock的lockInterruptibly()方法源码分析
Pytest学习-setup/teardown
P1449 后缀表达式
(PC+WAP)织梦模板螺钉手柄类网站
剑指offer第22题-链表中倒数第K个节点
Kotlin - extension functions and operator overloading
Why do we need callbacks
"Digital Economy Panorama White Paper" Financial Digital User Chapter released!
图论-虚拟节点分层建图
Zilliz 2023 Fall Campus Recruitment Officially Launched!
【RYU】rest_router.py源码解析
RPA助力商超订单自动化!