Didart

Maximum Element

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