📍 문제 설명
https://programmers.co.kr/learn/courses/30/lessons/92334
코딩테스트 연습 - 신고 결과 받기
문제 설명 신입사원 무지는 게시판 불량 이용자를 신고하고 처리 결과를 메일로 발송하는 시스템을 개발하려 합니다. 무지가 개발하려는 시스템은 다음과 같습니다. 각 유저는 한 번에 한 명의
programmers.co.kr
💡 접근
처음에는 배열만을 이용해서 풀려고 했으나 3중 반복문을 써야하는 상황이 만들어졌다.
맵에 담고 2중 반복문으로 돌리니 시간초과가 뜨지 않았다.
👩💻 코드
import java.util.Arrays;
import java.util.HashMap;
class Solution {
public int[] solution(String[] id_list, String[] report, int k) {
int[] answer = new int[id_list.length];
//유저의 신고 당한 횟수 기록
HashMap<String, Integer> map = new HashMap<>();
String[] DeduplicationReport = Arrays.stream(report).distinct().toArray(String[]::new);
for(int i = 0; i < DeduplicationReport.length; i++) {
String[] s = DeduplicationReport[i].split(" ");
map.put(s[1], map.getOrDefault(s[1], 0) + 1);
}
for(int i = 0; i < DeduplicationReport.length; i++) {
String[] s = DeduplicationReport[i].split(" ");
//신고 당한 사람이고 그 사람이 정지될 사람이면
if(map.containsKey(s[1]) && map.get(s[1]) >= k) {
for(int j = 0; j < id_list.length; j++) {
if(id_list[j].equals(s[0])) {
answer[j]++;
break;
}
}
}
}
return answer;
}
public static void main(String[] args) {
Solution s = new Solution();
String[] id_list = new String[]{"muzi", "frodo", "apeach", "neo"};
String[] report = new String[]{"muzi frodo","apeach frodo","frodo neo","muzi neo","apeach muzi"};
int k = 2;
s.solution(id_list, report, k);
}
}
'Algorithm > programmers' 카테고리의 다른 글
[2017 카카오코드]카카오프렌즈 컬러링북 (0) | 2022.02.23 |
---|---|
[2019 카카오 블라인드]오픈채팅방 (0) | 2022.02.23 |
[2021 카카오 블라인드]신규 아이디 추천 (0) | 2022.02.22 |
[2020 카카오 인턴십]키패드 누르기 (0) | 2021.12.05 |
[2018 카카오 블라인드 1차]비밀지도 (0) | 2021.12.04 |