🎉 One-stop destination for all your technical interview Preparation 🎉
Design a Data Structure SpecialStack that supports all the stack operations like push(), pop(), isEmpty(), isFull() and an additional operation getMin() which should return minimum element from the SpecialStack
stack<int> s, ss;
void push(int a)
{
s.push(a);
if (ss.empty() || ss.top() >= a)
ss.push(a);
return;
}
int pop()
{
if (s.empty())
return -1;
int ans = s.top();
s.pop();
if (ss.top() == ans)
ss.pop();
return ans;
}
int getMin()
{
if (ss.empty())
return -1;
return ss.top();
}
**
stack<int> s;
int minElem = INT_MAX;
int getMin()
{
if (s.empty())
return -1;
return s.top();
}
/*returns poped element from stack*/
int pop()
{
if (s.empty())
return -1;
s.pop();
int temp = s.top();
s.pop();
if (!s.empty())
minElem = s.top();
return temp;
}
/*push element x into the stack*/
void push(int x)
{
if (s.empty())
minElem = INT_MAX;
if (x < minElem)
minElem = x;
s.push(x);
s.push(minElem);
}
stack<int> s;
int minElem = INT_MAX;
int getMin()
{
if (s.empty())
return -1;
return minElem;
}
void push(int x)
{
if (s.empty())
{
s.push(x);
minElem = x;
}
else
{
if (x >= minElem)
s.push(x);
else
{
s.push((2 * x) - minElem);
minElem = x;
}
}
}
void pop()
{
if (s.empty())
return;
else
{
if (s.top() >= minElem)
s.pop();
else
{
minElem = 2 * minElem - s.top();
s.pop();
}
}
}
int top()
{
if (s.empty())
return -1;
else
{
if (s.top() >= minElem)
return s.top();
else
return minElem;
}
}
With O(n) space:
With O(1) space: