Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.Stack;
- public class MaximumElement {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int commandsNum = sc.nextInt();
- Stack<Integer> stack = new Stack<>(), maxStack = new Stack<>();
- int maxNumber = 0;
- for (int i = 0; i < commandsNum; i++) {
- int command = sc.nextInt();
- if (command == 1) {
- int value = sc.nextInt();
- if (maxNumber <= value) {
- maxNumber = value;
- maxStack.push(maxNumber);
- }
- stack.push(value);
- } else if (command == 2) {
- if (stack.pop() == maxNumber) {
- maxStack.pop();
- if (maxStack.size() > 0) {
- maxNumber = maxStack.peek();
- } else {
- maxNumber = 0;
- }
- }
- } else {
- System.out.println(maxNumber);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement