Algorithm/programmers

[2022 카카오 블라인드]신고 결과 받기

마닐라 2022. 2. 22. 15:08

📍 문제 설명

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);
    }
}