当前位置:网站首页>Sword finger offer:[day 1 stack and queue (simple)] --- > stack containing min function
Sword finger offer:[day 1 stack and queue (simple)] --- > stack containing min function
2022-06-28 21:46:00 【Love you, little pig】
List of articles
One 、 Title Description
Defines the data structure of the stack , Please implement a in this type that can get the minimum elements of the stack min The function is in the stack , call min、push And pop The time complexity of O ( 1 ) \rm{O(1)} O(1).
Example :
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.min(); --> return -3.
minStack.pop();
minStack.top(); --> return 0.
minStack.min(); --> return -2.
Tips :
The total number of calls to each function shall not exceed 20000 Time
Two 、 Thought analysis
notes : Some contents and pictures in the analysis of ideas are for reference. I'll make a self-help deduction for you, seniors , Thank them for their selfless dedication
analysis
Ordinary stack
push()andpop()The complexity of the function isO(1), And get the minimum value of the stackmin()The function needs to traverse the whole stack , The complexity isO(N).
This question needs tomin()The complexity of the function is reduced toO(1), Can be achieved by establishing an auxiliary stack .Stack 1Used to store all elements , Push push() function 、 Out of the stack pop() function 、 Access to the top top() Function remains normal .Stack 2OfTo the top of the stackAlways putStack 1OfThe smallest element, In this way... Can be achievedmin()FunctionalO(1)Complexity .
Function design
push(x) function
① Stack 1 You can stack normally
② Stack 2 You need to pay attention to , Stack 2 The top of the stack must be a stack 1 The smallest element of . Every time you stack , JudgeStack 2Is it empty . ifempty, WillxPush theStack 2. Ruozhan 2Not empty, JudgexAnd stack 2 The size relationship of the top elements of the stack , if x≤Stack 2 Top element of , WillxPush to stack 2.
pop() function
① Stack 1 It is OK to stack normally
② Stack 2 You need to pay attention to , Stack 1 Out of the stack 1 After elements , Stack 2 The top element of the stack should still be the stack 1 The smallest element of . Every time out of the stack , Mark the out of stack element asy, ifyEqual stack 2 Top element of , Then stack 2 Also perform stack out
top() function
Directly back to the stack 1 The top element of the stack
min() function
Directly back to the stack 2 The top element of the stack
Example :
3、 ... and 、 The overall code
The overall code is as follows
#define N 20000
// Create two stacks , Stack 1 It is used for normal stack in and stack out operations , Stack 2 Used to stack 1 The smallest element of is on the top of the stack
typedef struct {
int *Stack1;
int *Stack2;
int top1;
int top2;
} MinStack;
/** initialize your data structure here. */
MinStack* minStackCreate() {
MinStack* MS=(MinStack*)malloc(sizeof(MinStack));
MS->Stack1 = (int*)malloc(sizeof(int)*N);
MS->Stack2 = (int*)malloc(sizeof(int)*N);
MS->top1 = -1;
MS->top2 = -1;
return MS;
}
void minStackPush(MinStack* obj, int x) {
obj->Stack1[++obj->top1] = x; // Stack 1 Normal stack
if(obj->top2 == -1){
// If the stack 2 It's empty.
obj->Stack2[++obj->top2] = x; // take x Also push into the stack 2
}
else{
// If the stack 2 Not empty , meanwhile x<= Stack 2 Top element of
if(x <= obj->Stack2[obj->top2]){
// take x Push to stack 2 Top of stack
obj->Stack2[++obj->top2] = x;
}
}
}
void minStackPop(MinStack* obj) {
// Save stack 1 Top element of , meanwhile top1-1
int a = obj->Stack1[obj->top1];
obj->top1--;
// If the stack 1 The top element of the stack is equal to the stack 2 Top element of , Then stack 2 The top element of the stack pops up ,top2-1
if(a==obj->Stack2[obj->top2]){
obj->top2--;
}
}
// Return to the stack normally 1 The top element of the stack
int minStackTop(MinStack* obj) {
return obj->Stack1[obj->top1];
}
// Return to the stack normally 2 The top element of the stack
int minStackMin(MinStack* obj) {
return obj->Stack2[obj->top2];
}
void minStackFree(MinStack* obj) {
free(obj->Stack1);
free(obj->Stack2);
free(obj);
}
/** * Your MinStack struct will be instantiated and called as such: * MinStack* obj = minStackCreate(); * minStackPush(obj, x); * minStackPop(obj); * int param_3 = minStackTop(obj); * int param_4 = minStackMin(obj); * minStackFree(obj); */
function , Verification passed 
边栏推荐
- The rogue downloader named by 315 is back
- LeetCode122. 买卖股票的最佳时机II
- Proficient in data analysis, double the income? What is the strongest competitiveness
- [Note: circuit intégré MOS analogique] référence de bande Gap (principe de base + mode courant + circuit en mode tension)
- postman简介与安装步骤
- 力扣树的进一步应用
- LeetCode:合并K个升序链表_23
- LeetCode123. 买卖股票的最佳时机III
- LeetCode188. The best time to buy and sell stocks IV
- Query rewriting for opengauss kernel analysis
猜你喜欢

华为云的AI深潜之旅

Query rewriting for opengauss kernel analysis

Bitbucket failed to pull the warehouse Using SSH

Zero foundation self-study SQL course | complete collection of date functions in SQL

Recommend two high-quality Wallpaper software

MySQL system error occurred 1067

【激活函数】

Leetcode daily question - 515 Find the maximum value in each tree row

视觉弱监督学习研究进展

【筆記:模擬MOS集成電路】帶隙基准(基本原理+電流模+電壓模電路詳解)
随机推荐
LeetCode877. Stone game
Is it safe to open a dig money account? Is it reliable?
Figure neural network can also be used as CV backbone model. Huawei Noah Vig architecture is comparable to CNN and transformer
API gateway Apache APIs IX helps the evolution of snowball dual active architecture
LeetCode121. The best time to buy and sell stocks
LeetCode986. 区间列表的交集
Manual backup and restore of DHCP server
The further application of Li Kou tree
LeetCode121. 买卖股票的最佳时机
LeetCode1114. Print in sequence
LeetCode226. 翻转二叉树
LeetCode560. 和为K的子数组
安全 创新 实践|海泰方圆受邀参加“数字时代的网信创新与价值共创”技术交流研讨会
Leetcode daily question - 30 Concatenate substrings of all words
Deep interpretation of WiFi security vulnerability krack
Explanation and usage of sqrt() function
Pyechart drawing multiple Y-axis line graphs
LeetCode986. Intersection of interval lists
视觉弱监督学习研究进展
How to make up the PMP Exam? How much is the make-up exam?








