-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMin Stack.cpp
More file actions
69 lines (63 loc) · 1.66 KB
/
Min Stack.cpp
File metadata and controls
69 lines (63 loc) · 1.66 KB
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
// Problem: Min Stack
// Design a stack that supports push, pop, top, and getMin in O(1) time.
//
// Approach (Single Stack + Encoding):
// - Maintain a variable minVal to track the current minimum.
// - Instead of pushing values directly when a new minimum comes,
// encode it using: encodedVal = 2*val - minVal.
// This way, when popping, we can decode the previous min.
//
// push(val):
// - If stack empty: push val, set minVal = val.
// - If val >= minVal: push val normally.
// - If val < minVal: push encoded value (2*val - minVal),
// update minVal = val.
//
// pop():
// - If top < minVal: it means top is an encoded value.
// Restore previous min using: minVal = 2*minVal - top.
// - Pop the element.
//
// top():
// - If top < minVal: return minVal (since top is encoded).
// - Else, return top normally.
//
// getMin():
// - Return current minVal.
//
// Time Complexity: O(1) for all operations
// Space Complexity: O(n) for stack
class MinStack {
public:
stack<long long> s;
long long minVal;
MinStack() {}
void push(int val) {
if (s.empty()) {
s.push(val);
minVal = val;
} else if (val < minVal) {
// Encode value
s.push(2LL * val - minVal);
minVal = val;
} else {
s.push(val);
}
}
void pop() {
if (s.top() < minVal) {
// Restore previous min
minVal = 2 * minVal - s.top();
}
s.pop();
}
int top() {
if (s.top() < minVal) {
return minVal;
}
return s.top();
}
int getMin() {
return minVal;
}
};