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的更多相关文章

  1. Wrong Answer,Memory Limit Exceeded

    错误原因: 1.在递归的时候,递归函数中忘记加返回return. 1.1做题时遇到一个奇葩错误,把它记到这里,看代码: 代码1:错误,用c++提交wrong answer,但是用g++提交却accep ...

  2. Min Stack [LeetCode 155]

    1- 问题描述 Design a stack that supports push, pop, top, and retrieving the minimum element in constant ...

  3. Min Stack leetcode

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  4. leetCode Min Stack解决共享

    原标题:https://oj.leetcode.com/problems/min-stack/ Design a stack that supports push, pop, top, and ret ...

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

    这是悦乐书的第177次更新,第179篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第36题(顺位题号是155).设计一个支持push,pop,top和在恒定时间内检索最小 ...

  6. LeetCode 155:最小栈 Min Stack

    LeetCode 155:最小栈 Min Stack 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- ...

  7. [LeetCode] 0155. Min Stack 最小栈 & C++Runtime加速

    题目 Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. ...

  8. leetcode 155. Min Stack --------- java

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  9. Java [Leetcode 155]Min Stack

    题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant ti ...

随机推荐

  1. 纯css3实现tab选项卡

    <!doctype html> <html> <head> <title>Welcome</title> <meta http-equ ...

  2. CSS 实现底部固定

    在制作页面有这样一种现象:当一个HTML页面中含有较少的内容时,Web页面的“footer”部分随着飘上来,处在页面的半腰中间,给视觉效果带来极大的影响,让你的页面看上去很不好看,特别是现在宽屏越来越 ...

  3. VUE.JS组件化

    VUE.JS组件化 前言 公司目前制作一个H5活动,特别是有一定统一结构的活动,都要码一个重复的轮子.后来接到一个基于模板的活动设计系统的需求,便有了下面的内容.借油开车. 组件化 需求一到,接就是怎 ...

  4. jquery JS 左右方向键

    $(function (){ // $(document).keydown(function(e){ var code=e.which; switch (code) { case 38: //上 br ...

  5. sql大数据量查询的优化技巧

    1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...

  6. vs 2005 在IE下断点不起作用

    vs2005 加断点调试,ie下不起作用. 1. 点击[开始]->[运行] 命令:regedit. 2. 定位到HKEY_LOCALMACHINE -> SOFTWARE -> Mi ...

  7. vim常用命令总结 (转)

      在命令状态下对当前行用== (连按=两次), 或对多行用n==(n是自然数)表示自动缩进从当前行起的下面n行.你可以试试把代码缩进任意打乱再用n==排版,相当于一般IDE里的code format ...

  8. 浅谈Mybatis(二)

    一.resultMap 作用:发现数据库的查询结果与实体之间不匹配时,需要通过ResultMap来进行映射处理.常用于多表查询. 多表查询还是比较复杂的,因为可能的情况很多.这里只说两种情况: 1.1 ...

  9. Scala io操作

    1. 读文件 scala特有的是scala.io.Source,例如: import scala.io._ Source.fromFile(“cn.scala”,”utf8”).mkString 逐行 ...

  10. HDU2955-Robberies

    描述: The aspiring Roy the Robber has seen a lot of American movies, and knows that the bad guys usual ...