当前位置:网站首页>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(); */
边栏推荐
- # Arthas 简单使用说明
- 如何成为一名高级数字 IC 设计工程师(1-6)Verilog 编码语法篇:经典数字 IC 设计
- shake数据库中怎么使用Mongo-shake实现MongoDB的双向同步啊?
- H5 web player easyplayer How does JS realize live video real-time recording?
- Sublime Text4 download the view in bower and set the shortcut key
- 第一讲:包含min函数的栈
- Vs2013 generate solutions super slow solutions
- 【云原生】DevOps(一):DevOps介绍及Code工具使用
- Over 100000 words_ Ultra detailed SSM integration practice_ Manually implement permission management
- Information Security Experiment 4: implementation of IP packet monitoring program
猜你喜欢

【BW16 应用篇】安信可BW16模组/开发板AT指令实现MQTT通讯

Unity shader (to achieve a simple material effect with adjustable color attributes only)

Unity shader (basic concept)

Regular matching starts with XXX and ends with XXX

Variable parameter of variable length function

沙龙预告|GameFi 领域的瓶颈和解决方案

软件建模与分析

nlohmann json

正则匹配以XXX开头的,XXX结束的

How does mongodb realize the creation and deletion of databases, the creation of deletion tables, and the addition, deletion, modification and query of data
随机推荐
Oracle installation enhancements error
Unity uses mesh to realize real-time point cloud (I)
4、 Fundamentals of machine learning
華為HCIP-DATACOM-Core_03day
Pytest+request+allure+excel interface automatic construction from 0 to 1 [familiar with framework structure]
JWT certification used in DRF
Cesium does not support 4490 problem solution and cesium modified source code packaging scheme
二叉树高频题型
Interface test API case, data and interface separation
Unity shader (basic concept)
Sublime Text4 download the view in bower and set the shortcut key
【BW16 应用篇】安信可BW16模组/开发板AT指令实现MQTT通讯
Dynamics 365online applicationuser creation method change
Record of structured interview
创建一个长度为6的int型数组,要求数组元素的值都在1-30之间,且是随机赋值。同时,要求元素的值各不相同。
MongoDB怎么实现创建删除数据库、创建删除表、数据增删改查
Unity shader (pass user data to shader)
Pycharm create a new file and add author information
shake数据库中怎么使用Mongo-shake实现MongoDB的双向同步啊?
flinkcdc 用sqlclient可以指定mysqlbinlog id执行任务吗