desislava_topuzakova

03. Maximum Element

May 21st, 2021
774
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.01 KB | None | 0 0
  1. import java.util.ArrayDeque;
  2. import java.util.Collections;
  3. import java.util.Scanner;
  4. import java.util.Stack;
  5.  
  6. public class demo {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.         int n = Integer.parseInt(scanner.nextLine()); //броят на командите
  10.         ArrayDeque<Integer> stack = new ArrayDeque<>();
  11.  
  12.         for (int count = 1; count <= n ; count++) {
  13.             String command = scanner.nextLine();
  14.             //"1 98" -> split(" ") -> ["1", "98"]
  15.             //"2"
  16.             //"3"
  17.             if(command.equals("2")){
  18.                 stack.pop();
  19.             } else if (command.equals("3")) {
  20.                 if(!stack.isEmpty()) {
  21.                     System.out.println(Collections.max(stack));
  22.                 }
  23.             } else if (command.split(" ")[0].equals("1")) {
  24.                 int pushElement = Integer.parseInt(command.split(" ")[1]);
  25.                 stack.push(pushElement);
  26.             }
  27.         }
  28.  
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment