프로그래머스 - 신고 결과 받기

MINK·2022년 7월 2일
0

코딩테스트 준비

목록 보기
2/3
package hello;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

class Solution {
    public int[] solution(String[] id_list, String[] report, int k) {
        int[] answer = new int[id_list.length];

//      1. 중복제거
        HashSet<String> reportSet = new HashSet<String>();
        for(String rep : report) reportSet.add(rep);

//      2.notifyListHash 만들기
        HashMap<String, ArrayList<String>> notifyListHash = new HashMap<>();
        for (String rep : reportSet){
            int blankIdx = rep.indexOf(" ");
            String reporter = rep.substring(0, blankIdx);
            String reportee = rep.substring(blankIdx+1);

            ArrayList<String> reporterList = notifyListHash.getOrDefault(reportee, null);
            if (reporterList == null) reporterList = new ArrayList<>();

            reporterList.add(reporter);
            notifyListHash.put(reportee, reporterList);
        }

//      3. notifyListHash를 기반으로 reporterHash 만들기
        HashMap<String, Integer> reporterHash = new HashMap<>();
        for (ArrayList<String> notifyList : notifyListHash.values())
            if(notifyList.size() >= k)
                for(String reporter : notifyList)
                    reporterHash.put(reporter, reporterHash.getOrDefault(reporter,0) + 1);
//      4. reporterHash를 기반으로 answer 배열을 채움
        for(int i=0; i < id_list.length; i++)
            answer[i] = reporterHash.getOrDefault(id_list[i], 0);

        return answer;
    }
    public static void main(String[] args){
        Solution sol = new Solution();
        String[] id_list = {"muzi","frodo","apeach","neo"};
        String[] report = {"muzi frodo", "apeach frodo", "frodo neo", "muzi neo", "apeach muzi"};
        int k = 2;
        sol.solution(id_list, report, k);
    }


}

프로그래머스 - 신고 결과 받기
hash에 대한 이론 내용
신고 결과 받기에 대한 코드 풀이 설명

profile
Ethan Velog

0개의 댓글