We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent dce0bdb commit f15baa8Copy full SHA for f15baa8
155-min-stack/min-stack.java
@@ -0,0 +1,30 @@
1
+import java.util.Stack;
2
+
3
+public class MinStack {
4
5
+ Stack<Integer> stack = new Stack<>();
6
+ Stack<Integer> min_vals = new Stack<>();
7
8
9
+ public void push(int val) {
10
+ stack.push(val);
11
+ if(!min_vals.empty()){
12
+ min_vals.push(Math.min(val, min_vals.peek()));
13
+ }else{
14
+ min_vals.push(val);
15
+ }
16
17
18
+ public void pop() {
19
+ stack.pop();
20
+ min_vals.pop();
21
22
23
+ public int top() {
24
+ return stack.peek();
25
26
27
+ public int getMin() {
28
+ return min_vals.peek();
29
30
0 commit comments