[LeetCode] 0155. Min Stack 最小栈 & C++Runtime加速
题目
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
- push(x) -- Push element x onto stack.
- pop() -- Removes the element on top of the stack.
- top() -- Get the top element.
- getMin() -- Retrieve the minimum element in the stack.
设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈。
- push(x) -- 将元素 x 推入栈中。
- pop() -- 删除栈顶的元素。
- top() -- 获取栈顶元素。
- getMin() -- 检索栈中的最小元素。
Example:
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); --> Returns -3.
minStack.pop();
minStack.top(); --> Returns 0.
minStack.getMin(); --> Returns -2.
解法一
用两个栈就完事了,值得一说的是加速代码,暂时没找到什么空间复杂度很低的算法。
参考资料:https://blog.csdn.net/yujuan_mao/article/details/8119529
取消标准输入输出的同步,可以加速输入三倍(因为cin太慢啦),关掉cin和stdin的同步后,再把cin和cout的绑定也取消掉。瞬间提速……
static int x = [](){
std::ios::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
return NULL;
}();
class MinStack {
private:
stack<int> data;
stack<int> min_stack;
public:
/** initialize your data structure here. */
MinStack() = default;
void push(int x) {
data.push(x);
if (min_stack.empty() || min_stack.top() >= x) {
min_stack.push(x);
}
}
void pop() {
if (data.top() == min_stack.top()) {
min_stack.pop();
}
data.pop();
}
int top() {
return data.top();
}
int getMin() {
return min_stack.top();
}
};
运行结果
Runtime: 28 ms, faster than 98.82% of C++ online submissions for Min Stack.
Memory Usage: 17.2 MB, less than 12.90% of C++ online submissions for Min Stack.
[LeetCode] 0155. Min Stack 最小栈 & C++Runtime加速的更多相关文章
- [LeetCode] 155. Min Stack 最小栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- [CareerCup] 3.2 Min Stack 最小栈
3.2 How would you design a stack which, in addition to push and pop, also has a function min which r ...
- [LeetCode] Min Stack 最小栈
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- lintcode 中等题:Min stack 最小栈
题目 带最小值操作的栈 实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值. 你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成. 解题 可以定义 ...
- [LintCode] Min Stack 最小栈
Implement a stack with min() function, which will return the smallest number in the stack. It should ...
- 【LeetCode】155. Min Stack 最小栈 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 栈同时保存当前值和最小值 辅助栈 同步栈 不同步栈 日期 题目地 ...
- 第30题:LeetCode155. Min Stack最小栈
设计一个支持 push,pop,top 操作,并能在O(1)时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- 删除栈顶的元素. top() -- 获取栈顶元素 ...
- 155 Min Stack 最小栈
设计一个支持 push,pop,top 操作,并能在常量时间内检索最小元素的栈. push(x) -- 将元素x推入栈中. pop() -- 删除栈顶的元素. top() -- 获取 ...
- LeetCode之Min Stack 实现最小栈
LeetCode相关的网上资源比较多,看到题目一定要自己做一遍,然后去学习参考其他的解法. 链接: https://oj.leetcode.com/problems/min-stack/ 题目描述: ...
随机推荐
- QLineEdit 加省略号
第一种方法: QFontMetrics elidfont(ui->lineEdit->font()); ui->lineEdit->setText (elidfont.elid ...
- 【解决篇】映美FP-530K+打印发票卡纸,色带安装问题
之前由于色带变浅了,而换了色带,后来出现了发票经常中间卡纸的状况,不过也不是不能打,只是偶尔出现,发现每次打,发票纸会撞击一下色带,导致有时候发票会被色带挡一下,导致中间卡纸,有时候又能过去.后来通过 ...
- C# 与 .NET Framework 对应关系
C#各版本新增加功能(系列文章) 本系列文章主要整理并介绍 C# 各版本的新增功能. C# 8.0 C#8.0 于 2019年4月 随 .NET Framework 4.8 与 Visual St ...
- Alpha版本2发布
0.日常开头 这个作业属于哪个课程 <课程的链接> 这个作业要求在哪里 <https://www.cnblogs.com/harry240/p/11524162.html> 团 ...
- Java开发笔记(一百二十一)AWT输入框
前面介绍了文本标签Label,该控件展示的文字是不可编辑的,若要用户在界面上输入文本,就得使用专门的编辑框控件.在AWT的控件家族当中,用作编辑框的有两种控件,分别是单行输入框TextField和多行 ...
- python基础 — 异常处理
什么是异常? 异常即是一个事件,该事件会在程序执行过程中发生,影响了程序的正常执行. 一般情况下,在Python无法正常处理程序时就会发生一个异常. 异常是Python对象,表示一个错误. 当Pyth ...
- MySQL运维中的Tips--持续更新
1.into outfile 生成sql:一般都是生成文本或者其他形式的文件,现在需要生成sql形式的文件.配置文件加secure_file_priv=''select concat('insert ...
- 【C#】上机实验三
实验1: 定义一个 TimePeiod 类 包含: hour , minute , second 实现时间的在 时分秒上的加法. using System; using System.Collecti ...
- [CF724G]Xor-matic Number of the Graph
题目大意:有一张$n$个点$m$条边的无向图,定义三元组$(u,v,s)$是有趣的,当且仅当有一条$u\to v$的路径,路径上所有边的异或和为$s$.问所有有趣的三元组的$s$之和.$n\leqsl ...
- 二分法在JavaScript中的应用实例
前言:原来一直对算法和数据结构望而却步,总觉得前端可能对这块要求不用那么高,但是随着开发经验的增长以及阅历的提升,发现算法和数据结构还是相当重要的,在一些复杂功能的研发中都可以看得到它们的身影.要想提 ...