Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
3.1 kB
2
Indexable
Never
import java.util.*;
import java.io.*;
public class Main{
    static class FastReader{
        BufferedReader br;
        StringTokenizer st;
        public FastReader(){
            br=new BufferedReader(new InputStreamReader(System.in));
        }
        String next(){
            while(st==null || !st.hasMoreTokens()){
                try {
                    st=new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
        int nextInt(){
            return Integer.parseInt(next());
        }
        long nextLong(){
            return Long.parseLong(next());
        }
        double nextDouble(){
            return Double.parseDouble(next());
        }
        String nextLine(){
            String str="";
            try {
                str=br.readLine().trim();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return str;
        }
    }
    static class FastWriter {
		private final BufferedWriter bw;
		public FastWriter() {
			this.bw = new BufferedWriter(new OutputStreamWriter(System.out));
		}
		public void print(Object object) throws IOException {
			bw.append("" + object);
		}
		public void println(Object object) throws IOException {
			print(object);
			bw.append("\n");
		}
		public void close() throws IOException {
			bw.close();
		}
	}
    public static void main(String[] args) {
        try {
            FastReader sc=new FastReader();
            FastWriter out = new FastWriter();
            
            //write code here
            int t = sc.nextInt();
            Stack<Integer> stk = new Stack<>();
            Stack<Integer> stk1 = new Stack<>();
    	    HashMap<Integer, Integer> map = new HashMap<>();
            while(t>0){
        	    String str = sc.next();
        	   // System.out.println(stk);
        	   // System.out.println(stk1);
        	   // System.out.println(map);
                if(str.equals("push")){
                    int n = sc.nextInt();
                    stk1.push(n);
                    map.putIfAbsent(n,0);
                    map.put(n,map.get(n)+1);
                    if(!stk.isEmpty() && stk.peek() < n){
                    }
                    else{
                        stk.push(n);
                    }
                }
                else if(str.equals("getMin")){
                    while(map.get(stk.peek()) == 0){
                            stk.pop();
                        }
                    System.out.println(stk.peek());
                }
                else{
                    if(!stk1.isEmpty()){
                        int a = stk1.pop();
                        map.put(a,map.get(a)-1);
                    }
                }
                t--;
            }
            out.close();
        
        }
        catch (Exception e) {
            e.printStackTrace() ;
            return;
        }
    }
    
}
Leave a Comment