-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaximumElement.java
36 lines (33 loc) · 1.2 KB
/
MaximumElement.java
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
package StackAndQueues_Exercises;
import java.util.ArrayDeque;
import java.util.Scanner;
public class MaximumElement {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int numberOfCommands = Integer.parseInt(scan.nextLine());
ArrayDeque <Integer> stack = new ArrayDeque<>();
while(numberOfCommands > 0){
String command [] = scan.nextLine().split(" ");
String firstCommand = command[0];
switch (firstCommand){
case "1":
int secondCommand = Integer.parseInt(command[1]);
stack.push(secondCommand);
break;
case "2":
stack.pop();
break;
case "3":
int max = Integer.MIN_VALUE;
for (int element : stack){
if (element > max) {
max = element;
}
}
System.out.println(max);
break;
}
numberOfCommands--;
}
}
}