자료구조&알고리즘/백준

10815번: 숫자 카드 [JAVA]

yong_ღ'ᴗ'ღ 2023. 7. 16. 21:49

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

 

10815번: 숫자 카드

첫째 줄에 상근이가 가지고 있는 숫자 카드의 개수 N(1 ≤ N ≤ 500,000)이 주어진다. 둘째 줄에는 숫자 카드에 적혀있는 정수가 주어진다. 숫자 카드에 적혀있는 수는 -10,000,000보다 크거나 같고, 10,

www.acmicpc.net

접근방식)

1. 상근이의 카드 HashSet에 저장

2. 숫자 카드 배열 돌면서 hashSet.contains로 있는지 확인

** HashSet: contains() 시간복잡도 → O(1)

 

package codingTestStudy.week1;

import java.io.*;
import java.util.*;
public class B_10815 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());

        // 상근이가 가지고 있는 숫자 카드 추가
        HashSet<Integer> set = new HashSet<>();
        StringTokenizer st = new StringTokenizer(br.readLine());
        for (int i = 0; i < N; i++) {
            set.add(Integer.parseInt(st.nextToken()));
        }

        // 입력받은 카드 갖고 있는지 확인
        StringBuilder sb = new StringBuilder();
        int M = Integer.parseInt(br.readLine());
        st = new StringTokenizer(br.readLine());
        for (int i = 0; i < M; i++) {
            int card = Integer.parseInt(st.nextToken());
            if (set.contains(card))
                sb.append(1).append(' ');
            else
                sb.append(0).append(' ');
        }
        System.out.println(sb);
    }
}

 

+) 참고: java collections 시간 복잡도

https://www.grepiu.com/post/9

 

GrepIU

 

www.grepiu.com