-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMaximum Element.java
More file actions
49 lines (37 loc) · 1.16 KB
/
Maximum Element.java
File metadata and controls
49 lines (37 loc) · 1.16 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
47
48
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Stack<Integer>stack=new Stack<Integer>();
Stack<Integer>MaxStack=new Stack<Integer>();
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
for(int i=0;i<n;i++)
{
int query=scan.nextInt();
switch (query) {
case 1:
int x = scan.nextInt();
stack.push(x);
if (MaxStack.isEmpty()|| x>=MaxStack.peek())
{
MaxStack.push(x);
}
break;
case 2:
int poppedValue=stack.pop();
if (poppedValue == MaxStack.peek()) {
MaxStack.pop();
}
break;
case 3:
System.out.println(MaxStack.peek());
break;
default:
break;
}
}
scan.close();
}
}