-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaximum Element.java
More file actions
46 lines (38 loc) · 1.25 KB
/
Maximum Element.java
File metadata and controls
46 lines (38 loc) · 1.25 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.io.*;
import java.util.*;
public class Solution {
private static void getMaxElementFromStack()
{
Stack<Integer> stack = new Stack<Integer>();
Stack<Integer> onlyMaxs = new Stack<Integer>();
Scanner sc = new Scanner(System.in);
int N = Integer.parseInt(sc.nextLine());
int temp = 0;
while(sc.hasNext())
{
String type[] = sc.nextLine().split(" ");
switch(type[0])
{
case "1":
temp = Integer.parseInt(type[1]);
stack.push(temp);
if(onlyMaxs.isEmpty() || onlyMaxs.peek() <= temp)
onlyMaxs.push(temp);
break;
case "2":
temp = stack.pop();
if(temp == onlyMaxs.peek())
onlyMaxs.pop();
break;
case "3":
System.out.println(onlyMaxs.peek());
}
N--;
}
while(N-- > 0)
System.out.println(onlyMaxs.peek());
}
public static void main(String[] args) {
getMaxElementFromStack();
}
}