当前位置:网站首页>Force deduction 155 questions, minimum stack
Force deduction 155 questions, minimum stack
2022-07-26 23:38:00 【Yingtai night snow】
Power button 155 topic , Smallest stack
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 :
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 .
I/o sample
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.
Solution 1 : Regardless of time complexity , Use vector
class MinStack{
public:
MinStack()
{
nums={
};
}
void push(int val)
{
nums.push_back(val);
}
void pop()
{
nums.pop_back();
}
int top()
{
// cout<<*(nums.end()-1)<<endl;
return *(nums.end()-1);
}
int getMin()
{
int length=nums.size();
int minValue=nums[0];
for(int i=0;i<length;i++)
{
minValue=min(minValue,nums[i]);
}
// cout<<minValue<<endl;
return minValue;
}
private:
vector<int>nums;
};
Solution 2 : Use the auxiliary stack
// Use the auxiliary stack , One as the data stack , One as the smallest stack
class MinStack2{
private:
stack<int>dataStk;
stack<int>minStk;
public:
MinStack2()
{
// The minimum stack initializes a maximum value into the stack first
minStk.push(INT_MAX);
}
void push(int val)
{
dataStk.push(val);
minStk.push(min(minStk.top(),val));
}
void pop()
{
dataStk.pop();
minStk.pop();
}
int top()
{
// cout<<dataStk.top()<<endl;
return dataStk.top();
}
int getMin()
{
// cout<<minStk.top()<<endl;
return minStk.top();
}
};
Solution 3 , No auxiliary stack , Do not use extra space , Stack save difference
// Not applicable for additional space
class MinStack3
{
private:
int minValue=NULL;
stack<long>dataStk;
public:
MinStack3()
{
}
void push(int val)
{
if(dataStk.empty())
{
dataStk.push(0L);
minValue=val;
}
else
{
// What is stored in the data stack is the difference between the number and the minimum
dataStk.push(long(val)-minValue);
minValue=min(val,minValue);
}
}
// If interpolation diff Greater than or equal to 0, It indicates that the value to be published is greater than or equal to the current min, Then the value to be out of the stack is not updated when entering the stack min, return min+diff
// If interpolation diff Less than 0, It indicates that the current value to be published is min( Because when entering the stack, we choose min And the minimum value of the stack element ), meanwhile , adopt min-diff Before calculating min
// It should be noted that diff May exceed int Range , Be similar to Integer.MAX_VALUE - 1 such , therefore diff Use Long save
void pop()
{
long diff=dataStk.top();
dataStk.pop();
if(diff>=0)
{
}
else
{
int res=minValue;
minValue=minValue-diff;
}
}
int top()
{
long diff=dataStk.top();
if(diff>=0)
{
// cout<<minValue+diff<<endl;
return minValue+diff;
}
else{
// cout<<minValue<<endl;
return minValue;
}
}
int getMin()
{
// cout<<minValue<<endl;
return minValue;
}
};
边栏推荐
- Part II - C language improvement_ 10. Function pointer and callback function
- Interview questions of Bank of Hangzhou [Hangzhou multi tester] [Hangzhou multi tester _ Wang Sir]
- C.Net timestamp and time conversion support time zone
- Six challenges facing enterprise data governance!
- 告别宽表,用 DQL 成就新一代 BI
- JUnit、JMockit、Mockito、PowerMockito
- Silicon Valley class lesson 5 - Tencent cloud object storage and course classification management
- [2016] [paper notes] differential frequency tunable THz technology——
- Problems and solutions encountered in using nextline(), nextint() and next() in scanner
- Part II - C language improvement_ 5. Bit operation
猜你喜欢

SQL 基础知识

Part II - C language improvement_ 12. Packaging and use of dynamic / precision Library

Dynamic memory management and related topics

Import of MySQL data
![[shader realizes swaying effect _shader effect Chapter 4]](/img/ab/bdbc4a0f297541b532af81a49e2633.png)
[shader realizes swaying effect _shader effect Chapter 4]
![[flask advanced] analyze the thread isolation mechanism in flask in combination with the source code](/img/11/27d354a411358bfb39ae7126f33a37.png)
[flask advanced] analyze the thread isolation mechanism in flask in combination with the source code

Re understand the life world and ourselves

Cheaper than seals, with a large space for shape explosion. Is there really no match for 200000 or so? Chang'an's new "King fried" is cost-effective

百度网址收录

Real time voice quality monitoring
随机推荐
MySQL syntax uses detailed code version
Download win10 system image and create virtual machine on VMware virtual machine
Positioning of soaring problems caused by online MySQL CPU
Customer case | student education relies on observation cloud to create a new ecosystem of observable Smart Education
证券公司哪家佣金最低?网上开户安全吗
Simple SQL optimization
SQL 基础知识
pgsql -&gt; flink cdc -&gt; flink -&gt; Mysql, if a PgSQL CDC
[H5 bottom scrolling paging loading]
Three person management of system design
Learn various details and thoughts of chatroom implementation in Muduo
An online accident, I suddenly realized the essence of asynchrony
[MySQL] CentOS 7.9 installation and use mysql-5.7.39 binary version
第二部分—C语言提高篇_8. 文件操作
告别宽表,用 DQL 成就新一代 BI
Pytorch learning record (II): tensor
[postgresql]postgresqlg use generate_ Series() function completes statistics
30、 Modern storage system (management database and distributed storage system)
18. Opening and saving file dialog box usage notes
力扣152题:乘积最大子数组