신고 결과 받기

하이솝·2026년 3월 8일

2026.03.08

소요 시간: 23분

나의 정답
가장 많이 받은 선물과 유사한 형태의 문제라 똑같이
HashMap2차원 배열을 통해서 쉽게 해결할 수 있었음.
실력이 점차 느는 것 같아서 뿌듯함을 느낄 수 있었음

import java.util.Map;
import java.util.HashMap;
class Solution {
    public int[] solution(String[] id_list, String[] report, int k) {
        int[] answer = new int[id_list.length];
        for (int i = 0; i < answer.length; i++) {
            answer[i] = 0;
        }
        Map<String, Integer> nameId = new HashMap<>();
        // 해시맵에 이름과 인덱스 번호 저장
        for (int i = 0; i < id_list.length; i++) {
            nameId.put(id_list[i], i);
        }
        // 신고 여부 확인 배열 초기화
        boolean [][]reportCheck = new boolean[id_list.length][id_list.length];
        for (int i = 0; i < reportCheck.length; i++) {
            for (int j = 0; j < reportCheck[i].length; j++) {
                reportCheck[i][j] = false;
            }
        }
        int []reportCount = new int[id_list.length];
        for (int i = 0; i < reportCount.length; i++) {
            reportCount[i] = 0;
        }
        for (int i = 0; i < report.length; i++) {
            String []split = report[i].split(" ");
            int reporter = nameId.get(split[0]);
            int subject = nameId.get(split[1]);
            if (reportCheck[reporter][subject] == false) { // 아직 신고를 안했을 때
                reportCheck[reporter][subject] = true; // 신고 체크 후
                reportCount[subject]++; // 신고 횟수 증가
            }
        }
        for (int i = 0; i < reportCheck.length; i++) {
            for (int j = 0; j < reportCheck[i].length; j++) {
                if (reportCheck[i][j] == true && reportCount[j] >= k) {
                    answer[i]++;
                }
            } 
        }
        return answer;
    }
}

AI 정답
2차원 배열을 사용하게 되면 유저 수가 많아졌을 때, 메모리 사용량이 급격하게 늘어나게 된다는 문제가 발생할 수 있음
SetHashSet<>(Arrays.asList(report))를 통해
report 내부의 중복 신고 제거 후
신고당한 사람과 횟수를 countMap에 저장
LinkedHashMap을 사용해야 HashMap에 저장한 순서대로 값이 저장됨

Map<String, Integer> map = new HashMap<>();
map.put("A", 1);
map.put("B", 2);
map.put("C", 3);
map.put("D", 4);
map.put("E", 5);
System.out.println(map.keySet());
// 출력 결과(예시): [D, B, E, A, C] <- 순서가 엉망이 됨!

answerMap.values().stream().mapToInt(Integer::intValue).toArray(); 에서 .values()를 사용한 이유는 Map은 혼자서 Stream을 만들 수 없기 때문임

  • map.keySet(): Key들만 모아서 리스트가 필요할 때
    (리턴 타입: Set<K>)
  • map.values(): Value들만 모아서 리스트만 필요할 때
    (리턴 타입: Collection<V>)
  • map.entrySet(): Key와 Value 쌍으로 필요할 때
    (리턴 타입: Set<Entry<K,V>>)
import java.util.*;
class Solution {
    public int[] solution(String[] id_list, String[] report, int k) {
        // 1. 중복 신고를 아예 제거하고 시작 (report 배열 자체를 Set으로 변환)
        Set<String> distinctReport = new HashSet<>(Arrays.asList(report));
        // 2. 각 유저가 "신고 당한 횟수" 카운팅
        Map<String, Integer> countMap = new HashMap<>();
        for (String r : distinctReport) {
            String target = r.split(" ")[1];
            countMap.put(target, countMap.getOrDefault(target, 0) + 1);
        }
        // 3. 정지된 유저를 신고한 사람에게 메일 발송 횟수 계산
        Map<String, Integer> answerMap = new LinkedHashMap<>(); // 순서 보장을 위해 id_list 순서대로 0 세팅
        for (String id : id_list) answerMap.put(id, 0);
        for (String r : distinctReport) {
            String[] split = r.split(" ");
            String reporter = split[0];
            String target = split[1];
            if (countMap.get(target) >= k) {
                answerMap.put(reporter, answerMap.get(reporter) + 1);
            }
        }
        // 4. 결과값 배열로 변환
        return answerMap.values().stream().mapToInt(Integer::intValue).toArray();
    }
}

0개의 댓글