当前位置:网站首页>AcWing 41. Stack containing min function (monotone stack)
AcWing 41. Stack containing min function (monotone stack)
2022-06-12 10:40:00 【Jacob* ̄▽ ̄*】

Pre knowledge : Monotonic stack
The question :
The topic should be in O(1) All the operations described in the question can be realized within the time of .
Ideas :
In order to O(1) Time complexity of Realization getMin() operation , We also need to maintain another auxiliary stack B( Monotonic stack ), Stack B Is stored in Original stack A The minimum value of each piece of data starting at the bottom of the stack in the , The element has monotonicity .
How to maintain Monotonic stack Well ,
push: When we go to the stack Push the A few hours , If This number≤The top element of a monotonic stack , Then the number At the same time, press into the monotone stack ; otherwise , Do not press in , This is because the stack has First in, then out nature , therefore : In this number Before being ejected , In the stack There is always a number smaller than this number , therefore This number must not be taken as the minimum number Output .pop: When we get out of the stack eject A few hours , If This number is equal to the top element of the monotone stack , be At the same time, pop up the top element of the monotone stack .
Monotone stack because of its monotonicity , So its top element , Is the minimum number in the current stack .( This topic realizes O(1) The key to complexity )
Be careful :
- When pushing an element onto a monotone stack , The number must be “ Less than or equal to ” Monotone stack top element , Instead of “ Less than ” , namely The number of elements equal to the top of the stack should also be pushed onto the stack .
- hypothesis There are multiple elements in the original stack that are the same as the top element , When Original stack stay Pop up top element When , The auxiliary stack should also pop up this element , because There is only one such element in the auxiliary stack , It will not exist after deletion , This is not correct , Therefore, we should also put The same element as the top element of the monotone stack Join in Auxiliary stack in .
Time complexity :
O ( 1 ) O(1) O(1)
Code :( Array simulation stack implementation )
class MinStack {
public:
/** initialize your data structure here. */
int stk[110], mn[110];
int tt1 = 0, tt2 = 0;
MinStack() {
}
void push(int x) {
stk[++tt1] = x;
if (tt2 == 0) mn[++tt2] = x; // Insert the monotone stack directly when there are no elements
else {
if (x <= mn[tt2]) mn[++tt2] = x; // When there are elements, judge and then insert the monotone stack
else return;
}
}
void pop() {
--tt1;
if (stk[tt1 + 1] == mn[tt2]) --tt2;
}
int top() {
return stk[tt1];
}
int getMin() {
return mn[tt2];
}
};
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(x);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/
边栏推荐
- Collation of common functions in JS
- Leetcode 2169. 得到 0 的操作数
- Malicious code analysis practice -- using apatedns and inetsim to simulate network environment
- k53.第二章 基于二进制包安装kubernetes v1.22 --集群部署
- Malicious code analysis practice - lab03-02 DLL analysis
- Principle analysis of vite HMR
- 2021-03-24
- 人脸识别pip 安装dlib库失败
- Fiddler automatically saves the result of the specified request to a file
- PHP can load different new data methods for each refresh
猜你喜欢

How to play the 2022 Taobao 618 Super Cat Games? What are the strategies for the Super Cat Games

conda 安装tensorflow 测试tensorflow

AcWing 128. 编辑器(对顶栈 实现序列内部指定位置高效修改)

M-Arch(番外12)GD32L233评测-CAU加解密(捉弄下小编)

Leetcode2154. Multiply the found value by 2 (binary search)

Malicious code analysis practice - lab03-01 Exe basic dynamic analysis

基于QT的旅行查询与模拟系统

2022淘宝618超级喵运会玩法来了 超级喵运会有哪些攻略方法

Common methods of string class

2022 JD 618 Comment rembourser le dépôt de pré - vente? Le dépôt JD 618 peut - il être remboursé?
随机推荐
A few secrets - a special day
This and final keywords
Bug easily ignored by recursion
PHP wechat red packet allocation logic
力扣(LeetCode)162. 寻找峰值(2022.06.11)
Several methods of importing ThinkPHP
Leetcode 2169. Get operands of 0
How Qualcomm platform modifies special voltage
Fiddler automatically saves the result of the specified request to a file
k53.第二章 基于二进制包安装kubernetes v1.22 --集群部署
The most detailed explanation of the top ten levels of sqli labs platform
Common regular expressions
The difference between static method locking and non static method locking
CONDA install tensorflow test tensorflow
Pycharm view the current version of opencv
session_ start(): Cannot send session cache limiter - headers already sent
Pseudo static setting of access database in win2008 R2 iis7.5
Valentina Studio Pro for Mac(mac数据库管理软件)
PHP specifies the number of people to distribute the specified amount equally at random (scaling method)
MQTT 协议中文版