题目链接:https://leetcode-cn.com/problems/min-stack/description/

设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈。

push(x) -- 将元素 x 推入栈中。
pop() -- 删除栈顶的元素。
top() -- 获取栈顶元素。
getMin() -- 检索栈中的最小元素。

题解:

以往,我们经常用 $min[i]$ 维护区间 $[1,i]$ 的最小值,在这里我们同样也可以这么做。

AC代码(要想超过百分百的cpp提交记录,关闭IO同步是必不可少的……):

static const auto io_sync_off = []()
{
// turn off sync
std::ios::sync_with_stdio(false);
// untie in/out streams
std::cin.tie(nullptr);
return nullptr;
}();
#define maxn 10000
struct MinStack
{
int s[maxn],mn[maxn],tot;
MinStack() {
tot=;
}
void push(int x) {
s[tot]=x;
if(tot) mn[tot]=min(mn[tot-],x);
else mn[tot]=x;
tot++;
}
void pop() {
tot--;
}
int top() {
return s[tot-];
}
int getMin() {
return mn[tot-];
}
};

最新文章

  1. android持久化技术
  2. 检测cpu是否支持虚拟化和二级地址转换【转】
  3. java.lang.ClassCastException: com.sun.proxy.$Proxy32 cannot be cast to com.bkc.bpmp.core.cache.MemcachedManager
  4. Android View坐标getLeft, getRight, getTop, getBottom
  5. [转]CharacterController与Rigidbody
  6. (整理)ubuntu 的 相关知识(来自 鸟哥的私房菜)
  7. Docker存储驱动之AUFS简介
  8. x&(x-1)
  9. MySql解除安全模式:Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column.
  10. pyinstaller
  11. Ubuntu下使用QQ/Wechat
  12. oracle的DBMS_JOB相关知识
  13. 在VS2017上使用C#调用非托管C++生成的DLL文件(图文讲解)
  14. Python基础-day02
  15. linux下修改tomcat使用的jdk版本
  16. YQCB冲刺第二周第二天
  17. linux下mysql 启动命令
  18. 创建ASM实例及ASM数据库(转载)
  19. 【Linux】cd命令
  20. Python基础:文件的基本操作

热门文章

  1. mysql中TIMESTAMP设置默认时间为当前时间
  2. C#通过DSOFile读取与修改文件的属性
  3. 【转】java 读取 excel 2003 或 excel 2007
  4. tensorflow 笔记8:RNN、Lstm源码,训练代码输入输出,维度分析
  5. Android Studio原生库创建示例
  6. 【转】Centos7安装nodejs
  7. Android开发(十三)——全屏滚动与listview
  8. npm错误:Error: listen EADDRNOTAVAIL
  9. Spring Session Redis
  10. bootstrap学习-初步使用介绍