[프로그래머스] LV1. 신고 결과 받기

인스·2025년 7월 27일

💡 풀이

  • cntMap : 유저별 신고 당한 횟수 카운트
  • idMap : 유저별 신고한 ID 리스트로 넣기
  • reportSet : report 중복 제거
  • cntMap의 value를 확인하면서 k 이상 신고 당한 유저를 리스트에 넣기
  • idMap을 돌면서 유저가 신고한 ID 리스트 중에 정지당한 유저가 있으면 resCnt++
import java.util.*;

class Solution {
    public int[] solution(String[] id_list, String[] report, int k) {
        HashMap<String, Integer> cntMap = new HashMap<>(); // 신고당한 횟수
        HashMap<String, ArrayList<String>> idMap = new HashMap<>(); // 유저별 신고한 ID 
        HashSet<String> reportSet = new HashSet<>(); 
        
        // idMap에 유저 넣기
        for(int i = 0; i<id_list.length; i++){
            idMap.put(id_list[i], new ArrayList<String>());
        }
        // report 중복 제거
        for(String rep : report){
            reportSet.add(rep);
        }
        
        // 신고당한 횟수와 해당 유저가 신고한 ID 각각 해시맵에 넣기
        for(String rep : reportSet){
            String[] input = rep.split(" ");
            cntMap.put(input[1], cntMap.getOrDefault(input[1], 0) + 1);
            idMap.get(input[0]).add(input[1]);
        }
      
        // 신고당한 횟수가 k 이상인 유저 리스트에 넣기 
        ArrayList<String> banned = new ArrayList<>();
        for(String cnt : cntMap.keySet()){
            if (cntMap.get(cnt) >= k) banned.add(cnt);
        }

        // idMap을 돌면서 신고한 ID 중 정지당한 ID 있으면 count
        int[] answer = new int[id_list.length];
        for(int i = 0; i<id_list.length; i++){
            ArrayList<String> check = idMap.get(id_list[i]);
            int resCnt = 0;
            for(String c : check){
                if (banned.contains(c)) resCnt++;
            }
            answer[i] = resCnt;
        }
        
        return answer;
    }
}
profile
💻💡👻

0개의 댓글