Posts

Search

02x03 - The MaxMin Card Game

This card game begins with an empty deck.

There are following types of operations in the game :
Add X : Add a card with number X onto the top of the deck pile.
Remove : Remove the TopMost card from the deck pile.
CallMin : Find the minimum numbered card from the deck pile.
CallMax : Find the maximum numbered card from the deck pile.

You are given a sequence of operations of the game and your task is to write a program to play the game.

NOTE : You are expected to use the stack data structure only to get optimal solution. Other solutions may not be efficient enough.

INPUT

First line contains N the number of operations.
Next N lines each contain one operation each : "Add X", "Remove", "CallMax" or "CallMin" where X can be any non-negative integer.

OUTPUT

Output the minimum element whenever CallMin command is given, the maximum element whenever CallMax command is given, or "Invalid" whenever CallMin/CallMax/Remove command is given on an empty stack.

Sample Input 0

15
Add 1
Add 2
Add 3
CallMin
CallMax
Add 0
CallMin
CallMax
Remove
Remove
CallMin
Remove
Remove
Remove
CallMax

Sample Output 0

1
3
0
3
1
Invalid
Invalid

Solution :
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.util.ArrayList;
import java.util.List;
public class Solution {
    List<Node> items;
    public void push(int num) {
        if (items == null) {
            items = new ArrayList<Node>();
        }
        Node node = new Node(num);
        if (items.size() > 0) {
            node.min = Math.min(items.get(items.size() - 1).min, num);
            node.max = Math.max(items.get(items.size() - 1).max, num);

        } else {
            node.min = num;
            node.max = num;
        }
        items.add(node);
        
    }

    public Node pop() {
        Node popThis = null;
        if (items != null && items.size() > 0) {
            popThis = this.items.get(items.size() - 1);
            items.remove(items.size() - 1);         
        }
    else
        System.out.println("Invalid");
        return popThis;
    }

    public int getMin() {
    if (items != null && items.size() > 0) {
            int min = this.items.get(items.size() - 1).min;
            System.out.println(min);
            return min;
        }
    else
        System.out.println("Invalid");    
    
        return -1;

    }

    public int getMax() {
    if (items != null && items.size() > 0) {
            int max = this.items.get(items.size() - 1).max;
            System.out.println(max);
            return max;
        }
    else
        System.out.println("Invalid");        
        return -1;
    
    }

     public static void main(String args[]) {
        Solution stack = new Solution();
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
    while(a>0)
    {
        String s=sc.next();
        int tem;
        if(s.equals("Add")){
            tem = sc.nextInt();
            stack.push(tem);
        }
        else if(s.equals("CallMin")){
       stack.getMin();
        }
        else if(s.equals("CallMax")){
            stack.getMax();
        }
        else if(s.equals("Remove")){
            stack.pop();
        }
    a--;
        
    }
     }
}
class Node {

        int data;
        int min;
        int max;

        public Node(int data) {
            super();
            this.data = data;
        }

        public Node() {
            super();
        }
    }