You are viewing a single comment's thread. Return to all comments →
How about we just store the maximum values?
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { static class StackNode{ int val; StackNode(int val){ this.val = val; } } public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); Stack<StackNode> myStack = new Stack<>(); while(n-->0){ int type = in.nextInt(); if(type==1){ int x = in.nextInt(); myStack.push(new StackNode(myStack.empty()?x:Math.max(x, myStack.peek().val))); } else if(type==2) myStack.pop(); else System.out.println(myStack.peek().val); } } }
Seems like cookies are disabled on this browser, please enable them to open this website
Maximum Element
You are viewing a single comment's thread. Return to all comments →
How about we just store the maximum values?