-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day_76.cpp
81 lines (70 loc) · 1.54 KB
/
Day_76.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
DAY 76: Get minimum element from stack.
https://www.geeksforgeeks.org/design-a-stack-that-supports-getmin-in-o1-time-and-o1-extra-space/
QUESTION : You are given N elements and your task is to Implement a Stack in which
you can get minimum element in O(1) time.
Example 1:
Input:
push(2)
push(3)
pop()
getMin()
push(1)
getMin()
Output: 3 2 1
Explanation: In the first test case for query
push(2) the stack will be {2}
push(3) the stack will be {2 3}
pop() poped element will be 3 the
stack will be {2}
getMin() min element will be 2
push(1) the stack will be {2 1}
getMin() min element will be 1
Expected Time Complexity : O(1) for all the 3 methods.
Expected Auixilliary Space : O(1) for all the 3 methods.
Constraints:
1 <= Number of queries <= 100
1 <= values of the stack <= 100
*/
int _stack :: getMin()
{
if (s.empty()){
return -1;
}
else {
return minEle;
}
}
/*returns poped element from stack*/
int _stack ::pop()
{
if (s.empty()){
return -1;
}
int top = s.top();
s.pop();
if (top < minEle){
int k = minEle;
minEle = 2*minEle - top;
return k;
}
else{
return top;
}
}
/*push element x into the stack*/
void _stack::push(int x)
{
if (s.empty()){
minEle = x;
s.push(x);
return;
}
if (x < minEle){
s.push(2*x - minEle);
minEle = x;
}
else{
s.push(x);
}
}