[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/ 题目描述: ...
随机推荐
- [LeetCode] 90. Subsets II 子集合 II
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- 进程间之异步通信:信号Signal
信号 信号是进程间通信机制中唯一的异步通信机制:信号机制是进程间传递消息的一种机制,是异步进程中通信的一种方式 一个进程一旦接收到信号就会打断原来的程序执行流程来处理信号 内核处理一个进程收到的软中断 ...
- 【ARM-Linux开发】【CUDA开发】【深度学习与神经网络】Jetson Tx2安装相关之三
JetPack(Jetson SDK)是一个按需的一体化软件包,捆绑了NVIDIA®Jetson嵌入式平台的开发人员软件.JetPack 3.0包括对Jetson TX2 , Jetson TX1和J ...
- POJ 1014 Dividing(入门例题一)
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: Accepted: Description Marsha and Bill own ...
- Jenkins插件、war下载地址
1.jenkins的war包下载地址 地址:http://mirrors.jenkins-ci.org/,打开链接后,表格有war列,Releases行是短期更新包.LTS是长期更新包.一般选择Rel ...
- IDEA 获取类的相对路径和绝对路径
1.相对路径: 结果:action.HelloAction 2.绝对路径 结果:E:\javaProject\JavaEEProject\day08_Struts2_test01\src\action ...
- Word样式教程
目录 写在前面 样式可以解决什么问题? 本文适合于 快速入门 一切皆样式 样式与格式的关系 如何修改样式 建立新的样式 样式的匹配和更新 根据样式更新所选段落 根据所选段落更新样式 小结 进一步了解 ...
- SpringCloud——eureka集群
目的: 第一种普通方式 第二种方式骚操作 Eureka自我保护机制 Eureka集群搭建 说的通俗易懂一点就是,同一个项目部署在多个服务器上. 当注册中心扛不住高并发的时候,这时候 要用集群来扛: 今 ...
- 记一次git merge 事故
最近发生在自己身上的一件矬事儿,一不小心把matser上别人的代码给冲掉了,事后追溯了下原因. 1.准备三个分支,分别从master拉取 realease/v1.0分支 和 realease/bugf ...
- DevExtreme学习笔记(一) DataGrid中注意事项
1.阻止cell编辑 config.onEditorPreparing = function (e) { if (e.dataField === 'xx' && e.row.data. ...