[level 1] 신고 결과 받기 - 92334
성능 요약
메모리: 77 MB, 시간: 0.14 ms
구분
코딩테스트 연습 > 2022 KAKAO BLIND RECRUITMENT
채점결과
정확성: 100.0
합계: 100.0 / 100.0
문제 설명
문제 설명
신입사원 무지는 게시판 불량 이용자를 신고하고 처리 결과를 메일로 발송하는 시스템을 개발하려 합니다. 무지가 개발하려는 시스템은 다음과 같습니다.
- 각 유저는 한 번에 한 명의 유저를 신고할 수 있습니다.
- 신고 횟수에 제한은 없습니다. 서로 다른 유저를 계속해서 신고할 수 있습니다.
- 한 유저를 여러 번 신고할 수도 있지만, 동일한 유저에 대한 신고 횟수는 1회로 처리됩니다.
- k번 이상 신고된 유저는 게시판 이용이 정지되며, 해당 유저를 신고한 모든 유저에게 정지 사실을 메일로 발송합니다.
- 유저가 신고한 모든 내용을 취합하여 마지막에 한꺼번에 게시판 이용 정지를 시키면서 정지 메일을 발송합니다.
다음은 전체 유저 목록이 ["muzi", "frodo", "apeach", "neo"]이고, k = 2(즉, 2번 이상 신고당하면 이용 정지)인 경우의 예시입니다.
유저 ID | 유저가 신고한 ID | 설명 |
---|---|---|
"muzi" | "frodo" | "muzi"가 "frodo"를 신고했습니다. |
"apeach" | "frodo" | "apeach"가 "frodo"를 신고했습니다. |
"frodo" | "neo" | "frodo"가 "neo"를 신고했습니다. |
"muzi" | "neo" | "muzi"가 "neo"를 신고했습니다. |
"apeach" | "muzi" | "apeach"가 "muzi"를 신고했습니다. |
각 유저별로 신고당한 횟수는 다음과 같습니다.
유저 ID | 신고당한 횟수 |
---|---|
"muzi" | 1 |
"frodo" | 2 |
"apeach" | 0 |
"neo" | 2 |
위 예시에서는 2번 이상 신고당한 "frodo"와 "neo"의 게시판 이용이 정지됩니다. 이때, 각 유저별로 신고한 아이디와 정지된 아이디를 정리하면 다음과 같습니다.
유저 ID | 유저가 신고한 ID | 정지된 ID |
---|---|---|
"muzi" | ["frodo", "neo"] | ["frodo", "neo"] |
"frodo" | ["neo"] | ["neo"] |
"apeach" | ["muzi", "frodo"] | ["frodo"] |
"neo" | 없음 | 없음 |
따라서 "muzi"는 처리 결과 메일을 2회, "frodo"와 "apeach"는 각각 처리 결과 메일을 1회 받게 됩니다.
이용자의 ID가 담긴 문자열 배열 id_list
, 각 이용자가 신고한 이용자의 ID 정보가 담긴 문자열 배열 report
, 정지 기준이 되는 신고 횟수 k
가 매개변수로 주어질 때, 각 유저별로 처리 결과 메일을 받은 횟수를 배열에 담아 return 하도록 solution 함수를 완성해주세요.
제한사항
- 2 ≤
id_list
의 길이 ≤ 1,000- 1 ≤
id_list
의 원소 길이 ≤ 10 id_list
의 원소는 이용자의 id를 나타내는 문자열이며 알파벳 소문자로만 이루어져 있습니다.id_list
에는 같은 아이디가 중복해서 들어있지 않습니다.
- 1 ≤
- 1 ≤
report
의 길이 ≤ 200,000- 3 ≤
report
의 원소 길이 ≤ 21 report
의 원소는 "이용자id 신고한id"형태의 문자열입니다.- 예를 들어 "muzi frodo"의 경우 "muzi"가 "frodo"를 신고했다는 의미입니다.
- id는 알파벳 소문자로만 이루어져 있습니다.
- 이용자id와 신고한id는 공백(스페이스)하나로 구분되어 있습니다.
- 자기 자신을 신고하는 경우는 없습니다.
- 3 ≤
- 1 ≤
k
≤ 200,k
는 자연수입니다. - return 하는 배열은
id_list
에 담긴 id 순서대로 각 유저가 받은 결과 메일 수를 담으면 됩니다.
입출력 예
id_list | report | k | result |
---|---|---|---|
["muzi", "frodo", "apeach", "neo"] |
["muzi frodo","apeach frodo","frodo neo","muzi neo","apeach muzi"] |
2 | [2,1,1,0] |
["con", "ryan"] |
["ryan con", "ryan con", "ryan con", "ryan con"] |
3 | [0,0] |
입출력 예 설명
입출력 예 #1
문제의 예시와 같습니다.
입출력 예 #2
"ryan"이 "con"을 4번 신고했으나, 주어진 조건에 따라 한 유저가 같은 유저를 여러 번 신고한 경우는 신고 횟수 1회로 처리합니다. 따라서 "con"은 1회 신고당했습니다. 3번 이상 신고당한 이용자는 없으며, "con"과 "ryan"은 결과 메일을 받지 않습니다. 따라서 [0, 0]을 return 합니다.
제한시간 안내
- 정확성 테스트 : 10초
출처: 프로그래머스 코딩 테스트 연습, https://programmers.co.kr/learn/challenges
풀이
HashMap을 두 개 사용하여 풀었다
reportMap은 key를 유저로, value를 그 유저가 신고한 유저들(Set)로 하는 map이다.
reportCnt는 key를 유저, value는 그 유저가 신고당한 횟수를 저장하도록 하였다.
한 사람이 같은 사람을 여러번 신고할 수 없기 때문에 set으로 신고당한 유저들을 저장하고, set에 이미 저장되어 있는 유저는 cnt를 증가하지 않도록 하였다.
public static int[] solution(String[] id_list, String[] report, int k) {
Map<String, Set<String>> reportMap = new HashMap<>(); // key: 신고한 유저, value: 그 유저가 신고한 유저(신고당한 유저)
Map<String, Integer> reportCnt = new HashMap<>(); // key: 유저 이름, value: 신고받은 횟수
for (int i = 0; i < id_list.length; i++) {
reportMap.put(id_list[i], new HashSet<>());
reportCnt.put(id_list[i], 0);
}
for (int i = 0; i < report.length; i++) {
String[] names = report[i].split(" ");
String doReport = names[0];
String getReport = names[1];
Set<String> reportSet = reportMap.get(doReport);
if (reportSet.contains(getReport)) continue; // 같은 사람을 여러 번 신고했는지 체크
reportSet.add(getReport);
reportMap.put(doReport, reportSet);
/// 신고한 유저
reportCnt.put(getReport, reportCnt.get(getReport) + 1);
// 신고받은 횟수
}
int[] answer = new int[id_list.length];
for (int i = 0; i < id_list.length; i++) {
Set<String> reportSet = reportMap.get(id_list[i]);
for (String str : reportSet) {
int cnt = reportCnt.get(str);
if (cnt >= k) answer[i]++;
}
}
return answer;
}
'문제 풀이 > Programmers' 카테고리의 다른 글
[프로그래머스] 숫자 게임 (자바 Java) (0) | 2022.12.01 |
---|---|
[프로그래머스] 카카오프렌즈 컬러링북 (자바 Java) (0) | 2022.12.01 |
[프로그래머스] 비밀지도 (자바 Java) (0) | 2022.12.01 |
[프로그래머스] 프렌즈4블록 (자바 Java) (1) | 2022.11.03 |
[프로그래머스] 42888- 오픈채팅방 (자바 Java) (0) | 2022.10.13 |