์ฝ๋ฉํ
์คํธ/TIL
[99ํด๋ฝ] 10์ผ์ฐจ ๋ฌธ์ : ํ์
moon101
2025. 1. 24. 20:54
์ค๋์ ๋ฐฑ์ค ํ์ ๋ฌธ์ ์๊ณ ์ด๊ฒ๋ ํด์๋งต์ผ๋ก ๊ฐ๋จํ ํ ์ ์์๋ค.
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] nm = sc.nextLine().split(" ");
int N = Integer.parseInt(nm[0]);
int M = Integer.parseInt(nm[1]);
int result = 0;
Map<String, Integer> hm = new HashMap<>();
for(int i = 0; i < N; i++) {
int num = Integer.parseInt(sc.nextLine());
String[] students = sc.nextLine().split(" ");
for(int j = 0; j < num; j++) {
hm.put(students[j], hm.getOrDefault(students[j], 0) + 1);
}
}
for(Map.Entry<String, Integer> e : hm.entrySet()) {
if(e.getValue() >= M) {
result++;
}
}
// ๊ฒฐ๊ณผ ์ถ๋ ฅ
System.out.println(result);
}
}
ํด์๋งต ์ค์ ๋ฉ์๋ getOrDefault()๋ฅผ ์ด๋ป๊ฒ ์ฌ์ฉํ๋์ง, ํด์๋งต ๋ฐ๋ณต๋ฌธ์ ์ด๋ป๊ฒ ์์ฑํ๋์ง ์๋ฉด ์ฝ๊ฒ ํ ์ ์๋ ๋ฌธ์ ์๋ค. ํด์๋งต ๋ฐ๋ณต๋ฌธ ์์ฑํ๋ ๊ฑด ๊ณ์ ํท๊ฐ๋ ธ๋๋ฐ ๊ณ์ ์ฌ์ฉํ๋ค๋ณด๋ ์ด์ ์์ ํ ์ธ์ด ๊ฒ ๊ฐ๋ค.