


나의 풀이
import java.util.*;
class Solution {
public int[] solution(String[] id_list, String[] report, int k) {
HashMap<String, Integer> map1 = new HashMap<>(); // 1
HashMap<String, HashSet<String>> map2 = new HashMap<>();
for (int i = 0; i < id_list.length; i++) { // 2
map1.put(id_list[i], 0);
map2.put(id_list[i], new HashSet<>());
}
for (int i = 0; i < report.length; i++) { // 3
String[] str = report[i].split(" ");
String split1 = str[0];
String split2 = str[1];
if (map2.get(split1).add(split2)) { // 4
map1.put(split2, map1.get(split2) + 1);
}
}
int[] answer = new int[id_list.length];
for (int i = 0; i < id_list.length; i++) { // 5
HashSet<String> set = map2.get(id_list[i]);
int count = 0;
for (String s : set) {
if (map1.get(s) >= k) {
count++;
}
}
answer[i] = count;
}
return answer;
}
}
과정
- 신고당한 횟수와 신고한 사용자 목록을 담을 HashMap함수를 선언
- id_list의 정보를 map1과 map2에 담아준다
- report를 순회하여 공백을 기준으로 앞 문장과 뒷 문장으로 나눈다
- 한 사용자가 여러 번 신고한 횟수를 1로 초기화
- id_list를 순회하며 각 사용자가 신고한 다른 사용자들 중에 k번 이상 신고당한 결과를 계산하여 answer에 저장
다른 사람 풀이
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
class Solution {
public int[] solution(String[] id_list, String[] report, int k) {
List<String> list = Arrays.stream(report).distinct().collect(Collectors.toList());
HashMap<String, Integer> count = new HashMap<>();
for (String s : list) {
String target = s.split(" ")[1];
count.put(target, count.getOrDefault(target, 0) + 1);
}
return Arrays.stream(id_list).map(_user -> {
final String user = _user;
List<String> reportList = list.stream().filter(s -> s.startsWith(user + " ")).collect(Collectors.toList());
return reportList.stream().filter(s -> count.getOrDefault(s.split(" ")[1], 0) >= k).count();
}).mapToInt(Long::intValue).toArray();
}
}