-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetcode_155_Min_Stack.java
More file actions
48 lines (42 loc) · 1008 Bytes
/
Leetcode_155_Min_Stack.java
File metadata and controls
48 lines (42 loc) · 1008 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
41
42
43
44
45
46
47
48
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author KD
*/
import java.util.*;
public class Leetcode_155_Min_Stack {
Stack<TrackMin> st = new Stack<>();
// int newMin = Integer.MAX_VALUE;
/** initialize your data structure here. */
// public MinStack() {
//
// }
public void push(int x) {
int newMin = Integer.MAX_VALUE;
if(!st.empty()) newMin = st.peek().newMin;
if(newMin > x) newMin = x;
st.push(new TrackMin(x, newMin));
}
public void pop() {
if(!st.empty()) st.pop();
}
public int top() {
if(!st.empty()) return st.peek().val;
return 0;
}
public int getMin() {
return st.peek().newMin;
}
}
class TrackMin {
int val;
int newMin;
TrackMin(int v, int m){
val = v;
newMin = m;
}
}