18258큐 2

LJM·2022년 12월 30일
0

백준풀기

목록 보기
3/259

https://www.acmicpc.net/problem/18258

시간초과가 계속 되어서 별짓을 다했다
링크드리스트로 큐를 직접 구현해서 해봐도 안되고
배열기반으로 큐를 직접 구현해서 해봐도 안되고
ArrayList 도 안되고

결국 fast reader 라고 다른 유저가 만든 입력처리용 클래스에 내가 배열기반 큐를 직접구현해서 해보니 되었다...

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class Main {
    private static class MyQueue
    {
        ArrayList<Integer> arrList;

        MyQueue()
        {
            arrList = new ArrayList<>();
        }

        void push(int n)
        {
            arrList.add(n);
        }

        int pop()
        {
            if(1 == empty())
            {
                return -1;
            }

            return  arrList.remove(0);
        }

        int size()
        {
            return arrList.size();
        }

        int empty()
        {
            return (arrList.isEmpty()) ? 1 : 0;
        }

        int front()
        {
            if(empty()==1)
                return -1;

            return arrList.get(0);
        }

        int back()
        {
            if(empty()==1)
                return -1;

            return arrList.get(arrList.size()-1);
        }
    }


    public static void main(String[] args) {


        FastReader rd = new FastReader();

        StringBuilder ret = new StringBuilder();

        int inputCount;
        inputCount = rd.nextInt();

        int curCount = 0;

        MyQueue mq = new MyQueue();


        while(curCount < inputCount)
        {

            String[] Line = rd.nextLine().split(" ");
            String cmd = Line[0];

            if(cmd.equals("push"))
            {
                int num = Integer.parseInt(Line[1]);
                mq.push(num);
            }
            else if(cmd.equals("pop"))
            {
                ret.append(mq.pop());
                ret.append("\n");
            }
            else if(cmd.equals("size"))
            {
                ret.append(mq.size());
                ret.append("\n");
            }
            else if(cmd.equals("empty"))
            {
                ret.append(mq.empty());
                ret.append("\n");
            }
            else if(cmd.equals("front"))
            {
                ret.append(mq.front());
                ret.append("\n");
            }
            else if(cmd.equals("back"))
            {
                ret.append(mq.back());
                ret.append("\n");
            }

            curCount++;

        }
        System.out.println(ret);

    }


    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while(st == null || !st.hasMoreElements()) {
                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();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}
profile
게임개발자 백엔드개발자

0개의 댓글