본문 바로가기
알고리즘/프로그래머스

[프로그래머스] 신고 결과 받기(Java), HashSet 사용

by hxxyeoniii 2024. 10. 6.

링크 : https://school.programmers.co.kr/learn/courses/30/lessons/92334

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr


문제 풀이

import java.util.*;

class Solution {
    public int[] solution(String[] id_list, String[] report, int k) {
        
        int[] answer = new int[id_list.length];
        Map<String, HashSet<String>> reportedMap = new HashMap<>(); // 중복을 제거하기 위해 HashSet 사용
        Map<String, Integer> idxMap = new HashMap<>();
        
        for(int i=0; i<id_list.length; i++) {
            reportedMap.put(id_list[i], new HashSet<>());
            idxMap.put(id_list[i], i);
        }
        
        for(int i=0; i<report.length; i++) {
            String[] str = report[i].split(" "); // 띄어쓰기 기준 문자열 분리
            String a = str[0]; // 신고한 ID
            String b = str[1]; // 신고된 ID
            
            reportedMap.get(b).add(a);
        }
        
        for(int i=0; i<id_list.length; i++) {
            HashSet<String> send = reportedMap.get(id_list[i]);
            if(send.size() >= k) {
                for(String name : send) {
                    answer[idxMap.get(name)]++;
                }
            }
        }
        return answer;
    }
}