-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy pathProblem3.java
More file actions
40 lines (33 loc) · 877 Bytes
/
Problem3.java
File metadata and controls
40 lines (33 loc) · 877 Bytes
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
// Time Complexity: O(1)
// Space Complexity: O(N)
class MinStack {
private ArrayList<int[]> st;
public MinStack() {
st = new ArrayList<>();
}
public void push(int val) {
int[] top = st.isEmpty() ? new int[]{val, val} : st.get(st.size() - 1);
int min_value = top[1];
if (min_value > val) {
min_value = val;
}
st.add(new int[]{val, min_value});
}
public void pop() {
st.remove(st.size()-1);
}
public int top() {
return st.isEmpty() ? -1 : st.get(st.size() - 1)[0];
}
public int getMin() {
return st.isEmpty() ? -1 : st.get(st.size() - 1)[1];
}
}
/**
* Your MinStack object will be instantiated and called as such:
* MinStack obj = new MinStack();
* obj.push(val);
* obj.pop();
* int param_3 = obj.top();
* int param_4 = obj.getMin();
*/