Min Stack (LeetCode) tweak it to avoid Memory Limit Exceeded

class MinStack {
public:
void push(int x) {
if(values.empty())
{
values.push_back(x);
min_indices.push_back();
}
else
{
if( x < values[min_indices.back()] )
min_indices.push_back(values.size());
values.push_back(x);
}
}
void pop() {
values.pop_back();
if(values.size() == min_indices.back())
min_indices.pop_back();
}
int top() {
return values.back();
}
int getMin() {
return values[min_indices.back()];
}
private:
deque<int> values;
deque<deque<int>::size_type> min_indices;
};
Using the std::vector will lead to 'memory limit exceeded’, so i use the deque instead.
Min Stack (LeetCode) tweak it to avoid Memory Limit Exceeded的更多相关文章
- Wrong Answer,Memory Limit Exceeded
错误原因: 1.在递归的时候,递归函数中忘记加返回return. 1.1做题时遇到一个奇葩错误,把它记到这里,看代码: 代码1:错误,用c++提交wrong answer,但是用g++提交却accep ...
- Min Stack [LeetCode 155]
1- 问题描述 Design a stack that supports push, pop, top, and retrieving the minimum element in constant ...
- Min Stack leetcode
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- leetCode Min Stack解决共享
原标题:https://oj.leetcode.com/problems/min-stack/ Design a stack that supports push, pop, top, and ret ...
- LeetCode算法题-Min Stack(Java实现)
这是悦乐书的第177次更新,第179篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第36题(顺位题号是155).设计一个支持push,pop,top和在恒定时间内检索最小 ...
- LeetCode 155:最小栈 Min Stack
LeetCode 155:最小栈 Min Stack 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- ...
- [LeetCode] 0155. Min Stack 最小栈 & C++Runtime加速
题目 Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. ...
- leetcode 155. Min Stack --------- java
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- Java [Leetcode 155]Min Stack
题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant ti ...
随机推荐
- 关于wireshark的两个抓包过滤显示的基本语法
关于wireshark的两个基本语法 关于wireshark的两个基本语法 1. Capture Filters 语法:<Protocol name><Direction>&l ...
- avalon.js 多级下拉框实现
学习avalon.js的时候,有一个多级下拉框的例子,地址 戳这里 代码实现了联动, 但是逻辑上面理解有点难度,获取选择的值 和 页面初始化 功能存在问题. 在写地图编辑的时候,也用到了多级下拉框,特 ...
- struts2笔记03-ActionContext
1.概念 ActionContext是action的上下文,它包括action执行所需要的对象.struts2对每一个action都会创建一个新的ActionContext实例,同Action一样,是 ...
- 简易 Ajax 入门案例
AJAX = 异步 JavaScript 及 XML(Asynchronous JavaScript and XML) AJAX 不是一种新的编程语言,而是一种用于创建更好更快以及交互性更强的 Web ...
- “_In_opt_z_”: 未声明的标识符
问题 使用VS2010 + WDK 环境进行驱动开发时,编译阶段出现如下等错误提示 “_In_opt_z_”: 未声明的标识符 解决办法 将..\Microsoft Visual Studio 10. ...
- 强大的DELPHI RTTI–兼谈需要了解多种开发语言
一月 27th, 2005 by 猛禽 风焱在<“18般武艺”?>中说到他碰上的被多种语言纠缠的问题.我在回复里说: 很多语言只要能看懂几分就行了,没必要每一种都精通 但是如果只会很少的一 ...
- substr,substring,slice 的区别
javascript中的三个函数substr,substring,slice都可以用来提取字符串的某一部分(函数名称都是小写,不要写成subStr,subString又或者Substring,记住js ...
- poj1656---数黑格子
题意:有white,black,test操作 black将给定范围涂黑,white将给定范围涂白,test将给定范围的黑格子数出来并且输出 思路:无论哪个操作格子范围都在 (x,y) (x+L-1 ...
- 面向对象程序设计-C++_课时11new & delete
Dynamic memory allocation new new int; new Stash; new int[10]; new返回这个对象的指针 delete delete p; delete[ ...
- Wet Shark and Flowers(思维)
C. Wet Shark and Flowers time limit per test 2 seconds memory limit per test 256 megabytes input sta ...