-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTemperatureTracker.java
More file actions
31 lines (28 loc) · 1.07 KB
/
TemperatureTracker.java
File metadata and controls
31 lines (28 loc) · 1.07 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
import java.util.*;
public class TemperatureTracker {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.nextLine());
Stack<Integer> stack = new Stack<>();
Stack<Integer> minStack = new Stack<>();
while (n-- > 0) {
String[] input = sc.nextLine().split(" ");
switch (input[0]) {
case "PUSH":
int x = Integer.parseInt(input[1]);
stack.push(x);
if (minStack.isEmpty() || x <= minStack.peek()) minStack.push(x);
break;
case "POP":
if (!stack.isEmpty()) {
if (stack.peek().equals(minStack.peek())) minStack.pop();
stack.pop();
}
break;
case "MIN":
System.out.println(minStack.isEmpty() ? "EMPTY" : minStack.peek());
break;
}
}
}
}