当前位置:网站首页>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 - 1pop、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
边栏推荐
- Fluorescein-PEG-CLS, cholesterol-polyethylene glycol-fluorescein scientific research reagent
- RPA助力商超订单自动化!
- HCIP BGP实验报告
- The salary of soft testers at each stage, come to Kangkang, how much can you get?
- Kotlin - 扩展函数和运算符重载
- 3D 语义分割——2DPASS
- What is the difference between the generator version and the viewer version?
- Kotlin - extension functions and operator overloading
- complete binary tree problem
- Republish the lab report
猜你喜欢

射频芯片ATE测试从入门到放弃之参数测试

redis持久化方式

BMN: Boundary-Matching Network for Temporal Action Proposal Generation Reading Notes

Software testing is seriously involution, how to improve your competitiveness?

The Chinese Valentine's Day event is romantically launched, don't let the Internet slow down and miss the dark time

Fluorescein-PEG-CLS,胆固醇-聚乙二醇-荧光素科研试剂

最小化安装debian11

Fluorescein-PEG-CLS, cholesterol-polyethylene glycol-fluorescein scientific research reagent

易观分析:2022年Q2中国网络零售B2C市场交易规模达23444.7亿元

3D Semantic Segmentation - 2DPASS
随机推荐
Pytest学习-skip/skipif
V8中的快慢数组(附源码、图文更易理解)
设置工作模式与环境(下):探查和收集信息
Why Flutter Flutter of tutorials is the best choice for business?
The super perfect layout has shortcut keys and background replacement
HCIP BGP lab report
Creo 9.0二维草图的诊断:着色封闭环
Zilliz 2023 Fall Campus Recruitment Officially Launched!
jav一键生成数据库文档
Network basic learning series four (network layer, data link layer and some other important protocols or technologies)
[Paper Reading] TRO 2021: Fail-Safe Motion Planning for Online Verification of Autonomous Vehicles Using Conve
ML之interpret:基于titanic泰坦尼克是否获救二分类预测数据集利用interpret实现EBC模型可解释性之全局解释/局部解释案例
射频芯片ATE测试从入门到放弃之参数测试
代码重构:面向单元测试
Fluorescein-PEG-CLS,胆固醇-聚乙二醇-荧光素科研试剂
curl使用指南
什么是memoization,它有什么用?
utils 定时器
为什么我们需要回调
Quickly build a website with static files