当前位置:网站首页>Lecture 1: stack containing min function
Lecture 1: stack containing min function
2022-07-07 09:32:00 【Fight! Sao Nian!】
subject :AcWing 41. contain min Function of the stack
Design a support push,pop,top Wait for operation and can be in O(1) Retrieve the stack of the smallest elements in time .
push(x)– Put the element x Insert into the stack
pop()– Remove the top element
top()– Get the stack top element
getMin()– Get the smallest element in the stack
Data range
Total number of operation commands [0,100].
Examples
MinStack minStack = new MinStack();
minStack.push(-1);
minStack.push(3);
minStack.push(-4);
minStack.getMin(); --> Returns -4.
minStack.pop();
minStack.top(); --> Returns 3.
minStack.getMin(); --> Returns -1.
Topic analysis :
If this problem does not return the minimum value, then we can directly use the stack , What if you need to return the minimum value ?
We can use auxiliary stack
The sub group stack only stores elements smaller than the top element , Then the top of the auxiliary stack is the minimum value of the current stack .
According to the following figure, we can intuitively feel the relationship between stack and auxiliary stack .
Even if 3 Pop up stack , that 41 Will become the smallest value of the current stack .

class MinStack {
public:
/** initialize your data structure here. */
MinStack() {
}
void push(int x) {
stk.push(x);
if(min_stk.empty()||min_stk.top()>=x)min_stk.push(x);
}
void pop() {
if(min_stk.top()==stk.top())min_stk.pop();
stk.pop();
}
int top() {
return stk.top();
}
int getMin() {
return min_stk.top();
}
private:
stack<int> stk,min_stk;
};
/** * 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(); */
边栏推荐
- MySql数据库-事务-学习笔记
- Difference between interface iterator and iteratable
- IIS faked death this morning, various troubleshooting, has been solved
- stm32和电机开发(从单机版到网络化)
- 数据建模中利用3σ剔除异常值进行数据清洗
- **grafana安装**
- 4、 Fundamentals of machine learning
- JS judge whether checkbox is selected in the project
- 浏览器中如何让视频倍速播放
- Difference between process and thread
猜你喜欢

MySQL common statements

How does mongodb realize the creation and deletion of databases, the creation of deletion tables, and the addition, deletion, modification and query of data
![[SVN] what is SVN? How do you use it?](/img/45/a7df8989f18f0a6185582389398d1a.png)
[SVN] what is SVN? How do you use it?

Vs2013 generate solutions super slow solutions

Mysql database transaction learning notes

The configuration and options of save actions are explained in detail, and you won't be confused after reading it
![Pytest+request+allure+excel interface automatic construction from 0 to 1 [familiar with framework structure]](/img/33/9fde4bce4866b988dd2393a665a48c.jpg)
Pytest+request+allure+excel interface automatic construction from 0 to 1 [familiar with framework structure]

超十万字_超详细SSM整合实践_手动实现权限管理

Locust performance test 3 (high concurrency, parameter correlation, assembly point)

Nested (multi-level) childrn routes, query parameters, named routes, replace attribute, props configuration of routes, params parameters of routes
随机推荐
Implementation of corner badge of Youmeng message push
NETCORE 3.1 solves cross domain problems
Pycharm create a new file and add author information
Network request process
asp. How to call vb DLL function in net project
How to solve the problem of golang select mechanism and timeout
Jmeters use
四、机器学习基础
十二、排序
二叉树高频题型
牛客网——华为题库(61~70)
scrapy爬虫mysql,Django等
Unity3d interface is embedded in WPF interface (mouse and keyboard can respond normally)
Regular matching starts with XXX and ends with XXX
【SVN】SVN是什么?怎么使用?
Loxodonframework quick start
Skill review of test engineer before interview
Mysql:select ... for update
Kubernetes cluster capacity expansion to add node nodes
超十万字_超详细SSM整合实践_手动实现权限管理