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

 1 class MinStack {
 2 public:
 3     void push(int x) {
 4         if(values.empty())
 5         {
 6             values.push_back(x);
 7             min_indices.push_back(0);
 8         }
 9         else
10         {
11             if( x < values[min_indices.back()] )
12                 min_indices.push_back(values.size());
13             values.push_back(x);
14         }
15
16     }
17
18     void pop() {
19         values.pop_back();
20         if(values.size() == min_indices.back())
21             min_indices.pop_back();
22     }
23
24     int top() {
25         return values.back();
26     }
27
28     int getMin() {
29         return values[min_indices.back()];
30     }
31 private:
32     deque<int> values;
33     deque<deque<int>::size_type> min_indices;
34 };

using the std::vector will lead to ‘memory limit exceeded’, so i use the deque instead.

时间: 2024-11-10 11:33:40

Min Stack (LeetCode) tweak it to avoid Memory Limit Exceeded的相关文章

Min Stack leetcode

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 e

LeetCode[stack]: Min Stack

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 minim

LeetCode算法题-Min Stack(Java实现)

这是悦乐书的第177次更新,第179篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第36题(顺位题号是155).设计一个支持push,pop,top和在恒定时间内检索最小元素的堆栈. push(x) - 将元素x推入堆栈. pop() - 删除堆栈顶部的元素. top() - 获取顶部元素. getMin() - 检索堆栈中的最小元素. 例如: MinStack minStack = new MinStack(); minStack.push(-2); minSta

LeetCode: Min Stack 解题报告

Min Stack My Submissions Question Solution 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

[LeetCode] Min Stack Min Stack

Min Stack 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

Java [Leetcode 155]Min Stack

题目描述: 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 min

2016.6.1——Min Stack

Min Stack 本题收获: 1.可以利用两个栈操作. 2.栈的基本操作. 题目: 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

[LintCode] Min Stack 最小栈

Implement a stack with min() function, which will return the smallest number in the stack. It should support push, pop and min operation all in O(1) cost. Notice min operation will never be called if there is no number in the stack. Have you met this

[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 returns the minimum element? Push, pop and min should all operate in O(1) time. LeetCode上的原题,请参见我之前的博客Min Stack 最小栈.