Advertisement
Krassi_Daskalova

MaximumElement

Jan 29th, 2022
1,519
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.84 KB | None | 0 0
  1. import java.util.ArrayDeque;
  2. import java.util.Collections;
  3. import java.util.Scanner;
  4.  
  5. public class MaximumElement {
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         int numCommands = Integer.parseInt(scanner.nextLine());
  11.         ArrayDeque<Integer> stack = new ArrayDeque<>();
  12.  
  13.         for (int i = 1; i <= numCommands; i++) {
  14.             String command = scanner.nextLine();
  15.             int x = Integer.parseInt(command.split(" ")[0]);
  16.             if (x == 1) {
  17.                 int toPush = Integer.parseInt(command.split(" ")[1]);
  18.                 stack.push(toPush);
  19.             } else if (command.equals("2")){
  20.                 stack.pop();
  21.             } else if (command.equals("3")){
  22.                 System.out.println(Collections.max(stack));
  23.             }
  24.         }
  25.     }
  26. }
  27.  
  28.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement