当前位置:网站首页>Minimum stack < difficulty coefficient >
Minimum stack < difficulty coefficient >
2022-06-28 10:16:00 【Hua Weiyun】
Title Description : Design a support push ,pop ,top operation , And can retrieve the stack of the smallest elements in a constant time .
Realization MinStack class ( The time complexity of the following interfaces is required to be O(1)):
- MinStack() Initialize stack object .
- void push(int val) Put the element val Push to stack .
- void pop() Delete the element at the top of the stack .
- int top() Get the element at the top of the stack .
- int getMin() Get the smallest element in the stack .
Example 1:
Input :
[“MinStack”,“push”,“push”,“push”,“getMin”,“pop”,“top”,“getMin”]
[[],[-2],[0],[-3],[],[],[],[]]Output :
[null,null,null,null,-3,null,0,-2]explain :
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); -> return -3.
minStack.pop();
minStack.top(); -> return 0.
minStack.getMin(); -> return -2.
Tips :
- -2^31^ <= val <= 2^31^ - 1
- pop、top and getMin Operations are always called on non empty stacks
- push、pop、top、and getMin At most called 3 * 10^4^ Time
🧷 platform :Visual studio 2017 && windows
The core idea : Here it does not require us to use arrays or linked lists to implement natively , We use the stack in the library here , Just realize the interface function .
such as 【3, 8, 5, 0 ( Here first push 4 Data , Again pop A data )】, Second, define _min To record the minimum , Every time push Update when conditions are met _min, But when pop I'll put _min Delete the value of , The minimum value at this time is 3, But how do you write to know it's 3, You have to go through all the data in the stack , To know that the minimum value is 3, And at this time pop It is no longer O(1) 了 .

So our correct operation should be given to two stacks , A stack normal value , Another stack minimum ( Note that there are multiple minimum values here ), such as 【3, 8, 5, 0 ( Here first push 4 Data , Again pop A data )】, Here we are going to the first stack push Record the minimum value to the second stack 【3, 0】, If the values in the two stacks pop It's the same , Then all pop,【3】, Or just pop First stack . This is the classic idea of exchanging space for time .

Edge issues , such as 【3, 8, 5, 0 ( Here first push 4 Data , Again pop A data , Again push 0,4,0)】, the last one 0 need push Do you ? The answer is needed , If you don't push, Again pop The minimum value will be deleted ( Because the data at the top of the stack is the same ), here getMin Namely 5, But it's not .

leetcode The original title is
class MinStack {public: // You don't have to write its constructor here ( Delete it, too ok), because _st and _minst They are all custom types ( Call the default constructor initialization ), At the same time, there is no need to implement the destructor ( Call the default destructor ( Deconstruction of stack )), Similarly, copy construction and assignment do not need . MinStack() { } void push(int val) { _st.push(val); // Update stack if(_minst.empty() || val <= _minst.top()) { _minst.push(val); } } void pop() { //_st must pop, The same is all pop if(_st.top() == _minst.top()) { _minst.pop(); } _st.pop(); } int top() { return _st.top(); } int getMin() { //_minist The top of the stack is the current _st The minimum value of return _minst.top(); } stack<int> _st; stack<int> _minst;};/** * Your MinStack object will be instantiated and called as such: * MinStack* obj = new MinStack(); * obj->push(val); * obj->pop(); * int param_3 = obj->top(); * int param_4 = obj->getMin(); */边栏推荐
- ffmpeg录音录像
- 桥接模式(Bridge)
- mysql打不开,闪退
- Starting from full power to accelerate brand renewal, Chang'an electric and electrification products sound the "assembly number"
- Missed the golden three silver four, found a job for 4 months, interviewed 15 companies, and finally got 3 offers, ranking P7+
- Crawler small operation
- Au revoir! Navigateur ie, cette route Edge continue pour IE
- Huawei OSPF single region
- 一文读懂 12种卷积方法(含1x1卷积、转置卷积和深度可分离卷积等)
- 通过PyTorch构建的LeNet-5网络对手写数字进行训练和识别
猜你喜欢
![[unity][ecs] learning notes (I)](/img/eb/1f0ad817bbc441fd8c14d046b82dd0.png)
[unity][ecs] learning notes (I)

Starting from full power to accelerate brand renewal, Chang'an electric and electrification products sound the "assembly number"

为什么 Istio 要使用 SPIRE 做身份认证?

How to view the web password saved by Google browser

Numpy array: join, flatten, and add dimensions

Unity AssetBundle asset packaging and asset loading

dotnet 使用 Crossgen2 对 DLL 进行 ReadyToRun 提升启动性能

Several methods of using ABAP to operate Excel

Sqlcmd database connection error
![[Unity][ECS]学习笔记(二)](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[Unity][ECS]学习笔记(二)
随机推荐
[unity][ecs] learning notes (III)
What is the difference between MySQL development environment and test environment??
Why does istio use spirit for identity authentication?
What is the best way to learn machine learning
Unity AssetBundle asset packaging and asset loading
Summary of MySQL basic knowledge points
Ribbon核心源码解析
Instant messaging and BS architecture simulation of TCP practical cases
MySQL cannot be opened. Flash back
丢弃 Tkinter!简单配置快速生成超酷炫 GUI!
接口自动化框架脚手架-参数化工具的实现
桥接模式(Bridge)
Install using snap in opencloudos NET 6
Interface automation framework scaffold - use reflection mechanism to realize the unified initiator of the interface
Resolution: overview of decentralized hosting solution
SQL中的DQL、DML、DDL和DCL是怎么区分和定义的
Dbeaver installation and use tutorial (super detailed installation and use tutorial)
Cisco * VRF (virtual route forwarding table)
Adapter mode
[Unity]EBUSY: resource busy or locked