백준_18258_큐2

임정민·2023년 1월 24일
2

알고리즘 문제풀이

목록 보기
23/173
post-thumbnail

코딩테스트 연습 스터디 진행중 입니다. ✍✍✍
Notion : https://www.notion.so/1c911ca6572e4513bd8ed091aa508d67

문제

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

[나의 풀이]

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

class ringQue {

    public int[] que;
    private int front;
    private int rear;
    private int capacity;
    private int num;

    ringQue() {

        que = new int[2000000];
        capacity = 2000000;
        front = 0;
        rear = 0;
        num = 0;
    }

    public int push(int x) {
        if (num >= capacity) {
            return -1;
        }

        que[rear++] = x;
        num++;
        if (rear == capacity) {
            rear = 0;
        }

        return x;
    }

    public int pop() {
        if (num <= 0) {
            return -1;
        }

        int x = que[front++];
        que[front - 1] = 0;
        num--;
        if (front == capacity) {
            front = 0;
        }
        return x;
    }

    public int size() {
        return num;
    }

    public int empty() {
        return num <= 0 ? 1 : 0;
    }

    public int front() {
        int x = que[front];
        if (x == 0) {
            return -1;
        }
        return x;
    }

    public int back() {
        int x;
        if (rear == 0) {
            x = que[capacity - 1];
        } else {
            x = que[rear - 1];
        }
        if (x == 0) {
            return -1;
        }
        return x;
    }

}

public class Main {

    static ArrayList<String> getData() throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;
        ArrayList<String> list1 = new ArrayList<>();

        int n = Integer.parseInt(br.readLine());

        for (int i = 0; i < n; i++) {
            st = new StringTokenizer(br.readLine());
            String command = st.nextToken();
            if (command.equals("push")) {
                String num = st.nextToken();
                list1.add(command + num);
            } else {
                list1.add(command);
            }
        }

        return list1;
    }

    static void getAns(ArrayList<String> list1, ringQue ringQue) throws IOException {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        for (String command : list1) {
            String cmd = command.substring(0, 3);

            switch (cmd) {
                case "pus":
                    ringQue.push(Integer.parseInt(command.substring(4)));
                    break;
                case "pop":
                    bw.write(ringQue.pop() + "\n");
                    break;
                case "siz":
                    bw.write(ringQue.size() + "\n");
                    break;
                case "emp":
                    bw.write(ringQue.empty() + "\n");
                    break;
                case "fro":
                    bw.write(ringQue.front() + "\n");
                    break;
                case "bac":
                    bw.write(ringQue.back() + "\n");
                    break;
            }

        }
        bw.flush();
        bw.close();
    }

    public static void main(String[] args) throws NumberFormatException, IOException {

        ArrayList<String> list1 = new ArrayList<>();
        list1 = getData();

        ringQue ringque1 = new ringQue();
        getAns(list1, ringque1);

    }

}

링 버퍼 개념을 알고 있어 꽤 수월하게 풀 수 있었습니다. 답안 제출 중 계속해서 시간초과가 나는 경우가 있었습니다.

이때,

        for (String command : list1) {
            String cmd = command.replaceAll("[0-9]", "");

            switch (cmd) {

와 같은 코드에서 replaceAll이 시간이 많이 걸린다는 정보를 얻어

        for (String command : list1) {
            String cmd = command.substring(0,3);

            switch (cmd) {

위와 같이 substring으로 해결하였습니다.🍄🍄🍄

또한 실무에서는 지금과 같이 코드를 구조화시켜 협업/유지보수에 도움이 되게 작성하지만 이러한 문제들을 풀 때 시간단축하기 위해서는

  1. 입력/처리 한번에 진행
  2. 메서드 호출 지양
  3. 작업을 분할하지 않고 in=line으로 처리

를 중점으로 작성하는 것이 효율적이다 라는 의견을 받았습니다.

감사합니다!🥝🥝🥝

profile
https://github.com/min731

0개의 댓글