[Java] 백준 11723 집합

Lee GaEun·2025년 4월 22일

[Java] 알고리즘

목록 보기
61/93

11723 집합 문제 링크

문제


#1

import java.awt.*;
import java.io.*;
import java.util.*;

class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());
        String command;
        String[] newCom;
        Set<String> sList = new HashSet<>();

        for(int i=0; i<N; i++) {
            command = br.readLine();
            newCom = command.split(" ");

            if(newCom[0].equals("add")) {
                sList.add(newCom[1]);
            }
            else if (newCom[0].equals("check")) {
                if(sList.contains(newCom[1])) System.out.println(1);
                else System.out.println(0);
            }
            else if (newCom[0].equals("remove")) {
                sList.remove(newCom[1]);
            }
            else if (newCom[0].equals("toggle")) {
                if(sList.contains(newCom[1])) sList.remove(newCom[1]);
                else sList.add(newCom[1]);
            }
            else if (newCom[0].equals("all")) {
                for(int a=1; a<=20; a++) sList.add(String.valueOf(a));
            }
            else if (newCom[0].equals("empty")) {
                sList.clear();
            }

        }
    }
}

  • set으로 하면 될 줄 알았는데 시간 초과가 나네,,,

#2

import java.awt.*;
import java.io.*;
import java.util.*;

class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        int N = Integer.parseInt(br.readLine());
        String command;
        String[] newCom;
        boolean[] sList = new boolean[21];

        for(int i=0; i<N; i++) {
            command = br.readLine();
            newCom = command.split(" ");

            if(newCom[0].equals("add")) {
                sList[Integer.parseInt(newCom[1])] = true;
            }
            else if (newCom[0].equals("check")) {
                if(sList[Integer.parseInt(newCom[1])]) bw.write("1\n");
                else bw.write("0\n");
            }
            else if (newCom[0].equals("remove")) {
                sList[Integer.parseInt(newCom[1])] = false;
            }
            else if (newCom[0].equals("toggle")) {
                if(sList[Integer.parseInt(newCom[1])]) sList[Integer.parseInt(newCom[1])] = false;
                else sList[Integer.parseInt(newCom[1])] = true;
            }
            else if (newCom[0].equals("all")) {
                for(int a=1; a<=20; a++) Arrays.fill(sList, true);
            }
            else if (newCom[0].equals("empty")) {
                sList = new boolean[21];
            }
        }
        bw.flush();
        bw.close();
    }
}

  • 수의 범위값이 1~20이라고 정해져 있었네,,
  • 그럼 크기 정해져 있는 배열로 하는 게 더 빠르지 당근,,
  • 중복 체크도 안 하니까 boolean으로 하면 좀 더 빠를 듯
  • 성공..!
profile
I will give it my all (๑•̀o•́๑)ง

0개의 댓글