当前位置:网站首页>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
边栏推荐
- ML's yellowbrick: A case of interpretability (threshold map) for LoR logistic regression model using yellowbrick based on whether Titanic was rescued or not based on the two-class prediction dataset
- Analysys Analysis: The transaction scale of China's online retail B2C market in Q2 2022 will reach 2,344.47 billion yuan
- 2022/8/3 Exam Summary
- 剑指offer第22题-链表中倒数第K个节点
- JS获得URL超链接的参数值
- What is the difference between the generator version and the viewer version?
- Scala基础【正则表达式、框架式开发原则】
- 【开源框架】国内首个通用云计算框架,任意程序都可做成云计算。
- 【论文阅读】TRO 2021: Fail-Safe Motion Planning for Online Verification of Autonomous Vehicles Using Conve
- 3D Semantic Segmentation - 2DPASS
猜你喜欢
随机推荐
V8中的快慢数组(附源码、图文更易理解)
Cloud platform construction solutions
超级完美版布局有快捷键,有背景置换
websocket多线程发送消息报错TEXT_PARTIAL_WRITING--自旋锁替换synchronized独占锁的使用案例
Websocket multi-threaded sending message error TEXT_PARTIAL_WRITING--Use case of spin lock replacing synchronized exclusive lock
【论文阅读】TRO 2021: Fail-Safe Motion Planning for Online Verification of Autonomous Vehicles Using Conve
【深度学习】基于tensorflow的服装图像分类训练(数据集:Fashion-MNIST)
SRE运维解密-什么是SRE:DevOps模型的具体实践!
直播系统聊天技术(八):vivo直播系统中IM消息模块的架构实践
Click the icon in Canvas App to generate PDF and save it to Dataverse
The salary of soft testers at each stage, come to Kangkang, how much can you get?
《数字经济全景白皮书》金融数字用户篇 重磅发布!
RPA助力商超订单自动化!
Why do we need callbacks
Walk the Maze BFS
牛客2022 暑期多校3 H Hacker(SAM + 线段树查询区间内部最大子段和)
MCS-51单片机,定时1分钟,汇编程序
Shell编程之循环语句与函数
数据分析知识点搜集(纯粹的搜集)
云平台建设解决方案









