当前位置:网站首页>[Headline] Written test questions - minimum stack
[Headline] Written test questions - minimum stack
2022-08-02 00:15:00 【Chen Yikang】


Solution 1: (relative to solution 2, it will be very tasteless)
Thinking:
- Because the time complexity of obtaining the minimum element of the stack is O(1), all use a variable min to store the minimum value
- Set the variable min that stores the minimum value to Integer.MAX_VALUE
- In the push method, use min to compare each push
- Create two stacks, the first stack is used to store data, and the second stack is used to obtain the zero-hour stack with the minimum data through the first stack of variables
class MinStack {public Stack stack;public Stack stackTmp;public MinStack() {stack = new Stack<>();stackTmp = new Stack<>();}// first assign the maximum valuepublic int min = Integer.MAX_VALUE;public void push(int val) {// Assign a value if it is smaller than himif(val < min){min = val;}stack.push(val);}public void pop() {if(stack.pop() == min){//min needs to be reassigned after being popped, still using the maximum valuemin = Integer.MAX_VALUE;//Transfer all elements to stackTmp, the purpose is to make min re-comparison with all elementswhile(!stack.empty()){int tmp = stack.pop();if(tmp < min){min = tmp;}stackTmp.push(tmp);}// then move to stackwhile(!stackTmp.empty()){stack.push(stackTmp.pop());}}}public int top() {return stack.peek();}public int getMin() {return min;}} Solution 2: (analysis is as follows)





The code is as follows:
class MinStack {Stack stack;//Normal stackStack stackMin;//Minimum stackpublic MinStack() {stack = new Stack<>();stackMin = new Stack<>();}// push stackpublic void push(int val) {//Assign the initial value to the minimum stationif(stackMin.empty()){stackMin.push(val);}//If it is not the initial value, first judge whether it is less than or equal to (to prevent it from being empty after pop) the top element of the minimum stack, if so, push the stack to the minimum stackelse if(stackMin.peek() >= val){stackMin.push(val);}stack.push(val);}public void pop() {if((int)stackMin.peek() == (int)stack.peek()){stackMin.pop();}stack.pop();}public int top() {return stack.peek();}public int getMin() {return stackMin.peek();}} 边栏推荐
猜你喜欢

Flink Yarn Per Job - CliFrontend

如何优雅的消除系统重复代码

Wincc报表教程(SQL数据库的建立,wincc在数据库中保存和查询数据,调用Excel模板把数据保存到指定的位置和打印功能)

oozie startup error on cdh's hue, Cannot allocate containers as requested resource is greater than maximum allowed

GetHashCode与Equals

Axure tutorial - the new base (small white strongly recommended!!!)

短视频seo搜索优化主要内容

洞见云原生微服务及微服务架构浅析

Detailed explanation of Zadig's self-testing and tuning environment technical solution for developers

学习笔记:机器学习之回归
随机推荐
Artifact XXXwar exploded Artifact is being deployed, please wait...(已解决)
When Netflix's NFTs Forget Web2 Business Security
Axure教程-新手入门基础(小白强烈推荐!!!)
Wincc报表教程(SQL数据库的建立,wincc在数据库中保存和查询数据,调用Excel模板把数据保存到指定的位置和打印功能)
[头条]笔试题——最小栈
DOM 基础操作
Enterprise firewall management, what firewall management tools are there?
ansible模块--copy模块
SphereEx Miao Liyao: Database Mesh R&D Practice under Cloud Native Architecture
【Leetcode】1206. Design Skiplist
Thymeleaf简介
Win11如何获得最佳电源效率?
Docker实践经验:Docker 上部署 mysql8 主从复制
Detailed explanation of Zadig's self-testing and tuning environment technical solution for developers
C language Qixi is coming!It's time to show the romance of programmers!
PHP从txt文件中读取数据的方法
security 会话并发管理
中缀转后缀、前缀表达式快速解决办法
双队列实现栈?双栈实现队列?
C语言七夕来袭!是时候展现专属于程序员的浪漫了!