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

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.

上一篇:Container [pid=6263,containerID=container_1494900155967_0001_02_000001] is running beyond virtual memory limits


下一篇:e.next = nil // avoid memory leaks e.prev = nil // avoid memory leaks