Coding Test/JAVA 코딩테스트 풀이정리(프로그래머스)

프로그래머스 스쿨 Lv.1 - 신고결과받기(2022 KAKAO BLIND RECRUITMENT) // 시간초과 오류발생
깝몬 2023. 12. 15. 13:34
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

class Solution {
    public int[] solution(String[] id_list, String[] report, int k) {
        Set<String> reportSet = new HashSet<>(Arrays.asList(report));
        
        int[] reportCnt = new int[id_list.length];
        for(String value : reportSet){
            String[] valueSplit = value.split(" ");
            for(int i=0;i<id_list.length;i++){
                if(id_list[i].equals(valueSplit[1])){reportCnt[i]++;break;}
            }
        }
        
        for(int i : reportCnt) {if(i<=k) i=0; }
        
        int[] answer = new int[id_list.length];
        
        for(int i=0;i<reportCnt.length;i++){
           if(reportCnt[i]>=k){
               for(String value : reportSet){
                   String[] valueSplit = value.split(" ");
                   if(id_list[i].equals(valueSplit[1])){
                       for(int j=0;j<id_list.length;j++){
                           if(valueSplit[0].equals(id_list[j])){
                               answer[j]++;break;
                           }
                       }
                   }
               }
           } 
        }
        return answer;
    }
}

 

중복처리를 Set으로 하고 신고발생한 id를 토대로 그것과 일치하는 Set의 값을 찾아 그에 해당하는 신고자에게 1을 추가하려고 했는데.. 3중 for문인 탓인지 몇개의 테스트에서 시간초과로 떨어지고 말았다. 논리를 줄일 방법을 찾아 더 간단하게 줄일 방법을 생각해보아야겠다.

 

 

 

import java.util.*;

class Solution {
    public int[] solution(String[] id_list, String[] reports, int k) {
        Map<String, Set<String>> reportMap = new HashMap<>();
        Map<String, Set<String>> uniqueReportMap = new HashMap<>();
        Map<String, Integer> reportCountMap = new HashMap<>();

        // 각 유저가 신고한 유저를 저장하는 Map 생성
        for (String report : reports) {
            String[] reportSplit = report.split(" ");
            String reporter = reportSplit[0];
            String reported = reportSplit[1];

            reportMap.computeIfAbsent(reported, key -> new HashSet<>()).add(reporter);
        }

        // 중복된 신고를 1회로 처리하는 Map 생성
        for (Map.Entry<String, Set<String>> entry : reportMap.entrySet()) {
            Set<String> uniqueReporters = new HashSet<>(entry.getValue());
            uniqueReportMap.put(entry.getKey(), uniqueReporters);
            reportCountMap.put(entry.getKey(), uniqueReporters.size());
        }

        // 결과 메일을 받은 횟수 계산
        int[] answer = new int[id_list.length];
        for (int i = 0; i < id_list.length; i++) {
            String userId = id_list[i];
            int reportCount = reportCountMap.getOrDefault(userId, 0);

            if (reportCount >= k) {
                Set<String> reporters = uniqueReportMap.getOrDefault(userId, Collections.emptySet());
                for (String reporter : reporters) {
                    answer[Arrays.asList(id_list).indexOf(reporter)]++;
                }
            }
        }

        return answer;
    }
}

 

이것은 gpt를 통해 질문한 코드이다. hashmap을 이용한 후 중복처리 뒤에 신고횟수만큼 계산을 하였다.

 

 

 

for문 보단 stream을 배워 다시 한번 도전해봐야겠다..