Skip to main content
removed quotation formatting
Source Link
t3chb0t
  • 44.3k
  • 9
  • 84
  • 190

In this way we can get the maximum element in O(1) time. Whenever a new element is to be inserted we just check if the stack is not empty then compare it with the top of the stack and insert the element which is greatest of the top of the stack and the element to be inserted now. In this way, the greatest element will remain at the top which we can give back in O(1) time. Otherwise, if stack is empty then simply insert the element without doing anything.

In this way we can get the maximum element in O(1) time. Whenever a new element is to be inserted we just check if the stack is not empty then compare it with the top of the stack and insert the element which is greatest of the top of the stack and the element to be inserted now. In this way, the greatest element will remain at the top which we can give back in O(1) time. Otherwise, if stack is empty then simply insert the element without doing anything.

public class Solution {

public static void main(String[] args) {
   
    Scanner scan = new Scanner(System.in);
    Stack<Integer> stacky = new Stack<Integer>();
    int N = scan.nextInt();
    int ch;
    int x,ele=0;
    while(N-- >0){
      ch = scan.nextInt();
        if(ch==1){x = scan.nextInt();
            if(stacky.isEmpty()){
        stacky.push(x);
            }
            else{
                int y = stacky.peek();
                stacky.push(Math.max(x, y));
            }
        }

        if(ch==2){
            if(!stacky.isEmpty())
            stacky.pop();
       }
       
        if(ch==3){
      System.out.println(stacky.peek());
        }
    }}}

In this way we can get the maximum element in O(1) time. Whenever a new element is to be inserted we just check if the stack is not empty then compare it with the top of the stack and insert the element which is greatest of the top of the stack and the element to be inserted now. In this way, the greatest element will remain at the top which we can give back in O(1) time. Otherwise, if stack is empty then simply insert the element without doing anything.

public class Solution {

public static void main(String[] args) {
   
    Scanner scan = new Scanner(System.in);
    Stack<Integer> stacky = new Stack<Integer>();
    int N = scan.nextInt();
    int ch;
    int x,ele=0;
    while(N-- >0){
      ch = scan.nextInt();
        if(ch==1){x = scan.nextInt();
            if(stacky.isEmpty()){
        stacky.push(x);
            }
            else{
                int y = stacky.peek();
                stacky.push(Math.max(x, y));
            }
        }

        if(ch==2){
            if(!stacky.isEmpty())
            stacky.pop();
       }
       
        if(ch==3){
      System.out.println(stacky.peek());
        }
    }}}

In this way we can get the maximum element in O(1) time. Whenever a new element is to be inserted we just check if the stack is not empty then compare it with the top of the stack and insert the element which is greatest of the top of the stack and the element to be inserted now. In this way, the greatest element will remain at the top which we can give back in O(1) time. Otherwise, if stack is empty then simply insert the element without doing anything.

public class Solution {

public static void main(String[] args) {
   
    Scanner scan = new Scanner(System.in);
    Stack<Integer> stacky = new Stack<Integer>();
    int N = scan.nextInt();
    int ch;
    int x,ele=0;
    while(N-- >0){
      ch = scan.nextInt();
        if(ch==1){x = scan.nextInt();
            if(stacky.isEmpty()){
        stacky.push(x);
            }
            else{
                int y = stacky.peek();
                stacky.push(Math.max(x, y));
            }
        }

        if(ch==2){
            if(!stacky.isEmpty())
            stacky.pop();
       }
       
        if(ch==3){
      System.out.println(stacky.peek());
        }
    }}}
Source Link
akay
  • 21
  • 1

In this way we can get the maximum element in O(1) time. Whenever a new element is to be inserted we just check if the stack is not empty then compare it with the top of the stack and insert the element which is greatest of the top of the stack and the element to be inserted now. In this way, the greatest element will remain at the top which we can give back in O(1) time. Otherwise, if stack is empty then simply insert the element without doing anything.

public class Solution {

public static void main(String[] args) {
   
    Scanner scan = new Scanner(System.in);
    Stack<Integer> stacky = new Stack<Integer>();
    int N = scan.nextInt();
    int ch;
    int x,ele=0;
    while(N-- >0){
      ch = scan.nextInt();
        if(ch==1){x = scan.nextInt();
            if(stacky.isEmpty()){
        stacky.push(x);
            }
            else{
                int y = stacky.peek();
                stacky.push(Math.max(x, y));
            }
        }

        if(ch==2){
            if(!stacky.isEmpty())
            stacky.pop();
       }
       
        if(ch==3){
      System.out.println(stacky.peek());
        }
    }}}