개발 기록
[Programmers] 2022 KAKAO BLIND RECRUITMENT - 신고 결과 받기 본문
알고리즘 & 자료구조/baekjoon & programmers
[Programmers] 2022 KAKAO BLIND RECRUITMENT - 신고 결과 받기
1z 2024. 1. 21. 17:51
문제
: 게시판 불량 이용자 신고하고 처리 결과를 메일로 발송하는 시스템
■ 1. 문제 설명
* 각 유저는 한 번에 한 명의 유저를 신고할 수 있습니다.
* 신고 횟수에 제한은 없습니다. 서로 다른 유저를 계속해서 신고할 수 있습니다.
* 한 유저를 여러 번 신고할 수도 있지만, 동일한 유저에 대한 신고 횟수는 1회로 처리됩니다.
* k번 이상 신고된 유저는 게시판 이용이 정지되며, 해당 유저를 신고한 모든 유저에게 정지 사실을 메일로 발송합니다.
* 유저가 신고한 모든 내용을 취합하여 마지막에 한꺼번에 게시판 이용 정지를 시키면서 정지 메일을 발송합니다.
▶ id_list = 이용자의 ID가 담긴 문자열 배열 [muzi, frodo, apeach]
▶ report = 각 이용자가 신고한 이용자의 ID 정보가 담긴 문자열 배열 ["muzi frodo","apeach frodo", "frodo neo","muzi neo"]
▶ k = 정지 기준이 되는 신고 횟수 k
각 유저별로 신고한 아이디와 정지된 아이디를 정리하면 아래와 같다.

★ Solution ! 각 유저별로 처리 결과 메일을 받은 횟수를 배열에 담아 return 하도록 한다.

■ 2. 문제 풀이
(1) 각 유저별로 신고당한 횟수를 구한다.
// 각 유저별로 신고당한 횟수를 구한다.
HashMap<String, Integer> countMap = new HashMap<>();
for (String reportTxt : reportList) {
String target = reportTxt.split(" ")[1]; // 신고당한사람
countMap.put(target, count.getOrDefault(target, 0) + 1);
}
// countMap = {muzi=1, neo=2, frodo=2}
(2) 유저가 신고한 아이디들을 유저별 하나의 리스트로 묶는다.
Arrays.stream(id_list).map(userId -> {
final String user = userId;
// 각 유저별로 신고한 아이디 리스트업
List<String> userReportList = reportList.stream()
.filter(reportTxt -> reportTxt.startsWith(user + " "))
.collect(Collectors.toList());
// userReportList = [muzi frodo, muzi neo] -> 무지가 신고한 유저
// userReportList = [apeach frodo, apeach muzi] -> 아파치가 신고한 유저
// userReportList = [frodo neo] -> 프로도가 신고한 유저
(3) 유저가 신고한 아이디의 신고당한 횟수와 정기 기준이 되는 횟수 간의 비교
1. 유저가 신고한 아이디를 순차 조회하면서, => [muzi frodo, muzi neo]
2. 해당 아이디가 신고당한 횟수를 countMap 에서 가져오고, => countMap.get(frodo)
3. 정지 기준이 횟수와 비교한다. => CountMap.get(frodo) > 정지 기준 횟수
4. 정지기준이 되는 횟수보다 크거나 같을 경우, count 를 증가시킨다.
: 여기서 카운트는, 1번의 유저가 결과 처리 메일을 받을 횟수가 된다.
userReportList.stream()
.filter(userReportTxt -> countMap.getOrDefault(userReportTxt.split(" ")[1], 0) >= k)
.count();
★ 전체 코드
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) {
// 동일한 유저에 대한 신고 횟수는 1회로 처리되므로, 중복 신고값을 제거한다.
List<String> reportList = Arrays.stream(report).distinct().collect(Collectors.toList());
HashMap<String, Integer> countMap = new HashMap<>();
// 각 유저별로 신고당한 횟수를 구한다.
for (String reportTxt : reportList) {
String target = reportTxt.split(" ")[1]; // 신고당한사람
countMap.put(target, countMap.getOrDefault(target, 0) + 1);
}
int[] answer = Arrays.stream(id_list).map(userId -> {
final String user = userId;
// 각 유저별로 신고한 아이디 리스트업
List<String> userReportList = reportList.stream()
.filter(reportTxt -> reportTxt.startsWith(user + " "))
.collect(Collectors.toList());
// 유저가 신고한 아이디의 신고당한 횟수와, 정지 기준이 되는 신고 횟수 비교
return userReportList.stream()
.filter(userReportTxt -> countMap.getOrDefault(userReportTxt.split(" ")[1], 0) >= k)
.count();
}).mapToInt(Long::intValue).toArray();
return answer;
}
}
★ 결과
String[] id_list = {"muzi", "frodo", "apeach", "neo"};
String[] report = {"muzi frodo","apeach frodo","frodo neo","muzi neo","apeach muzi"};
int k = 2;

'알고리즘 & 자료구조 > baekjoon & programmers' 카테고리의 다른 글
| [BAEKJOON] 10872번 팩토리얼(Factorial) - 재귀 (0) | 2024.03.09 |
|---|---|
| [Programmers] 2024 KAKAO WINTER INTERNSHIP - 가장 많이 받는 선물 개수 (0) | 2024.01.22 |
| [Programmers] 2021 Dev-Matching - 로또의 최고 순위와 최저 순위 (0) | 2024.01.21 |
| [BAEKJOON] 2575번 N 번째 큰 수 (0) | 2023.11.16 |
| [BAEKJOON] 2581번 소수 구하기 (소수 합, 최소 소수 값) (0) | 2023.09.13 |