-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
44 lines (33 loc) · 904 Bytes
/
Solution.java
File metadata and controls
44 lines (33 loc) · 904 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
package org.example.problems.min_stack;
import org.example.problems.SolutionInterface;
import java.util.Stack;
public class Solution implements SolutionInterface {
@Override
public String getName() {
return "Min Stack";
}
@Override
public String getUrl() {
return "https://leetcode.com/problems/min-stack/";
}
private record ValueWithMin(int value, int minimum) {}
private final Stack<ValueWithMin> stack = new Stack<>();
public void push(int val) {
int min;
if (stack.empty()) {
min = val;
} else {
min = stack.peek().minimum;
}
stack.push(new ValueWithMin(val, Math.min(min, val)));
}
public void pop() {
stack.pop();
}
public int top() {
return stack.peek().value;
}
public int getMin() {
return stack.peek().minimum;
}
}