본문으로 바로가기
728x90
반응형
SMALL

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

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

 

프로그래머스

SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

 

 

 

걸린시간: 22분

import java.io.IOException;
import java.util.*;

class Solution {
        static Map<String, Integer> reportCounting = new HashMap<>();
    static Map<String, Information> info = new HashMap<>();
 public static int[] solution(String[] id_list, String[] report, int k) {
        int[] answer = new int[id_list.length];
        setUp(id_list);

        registReport(report);

        for (int i = 0; i < id_list.length; i++) {
            Set<String> repostList = info.get(id_list[i]).reportList;
            int cnt = 0;
            for (String name : repostList){
                if (reportCounting.get(name)>=k){
                    cnt++;
                }
            }
            answer[i]=cnt;
        }
        return answer;
    }

    static class Information {
        private Set<String> reportList = new HashSet<>();

        public void report(String name){
            reportList.add(name);
        }
    }

    static void setUp(String[] id_list){
        for (int i = 0; i < id_list.length; i++) {
            reportCounting.put(id_list[i],0);
            info.put(id_list[i],new Information());
        }
    }

    static void registReport(String[] report){
        for (int i = 0; i < report.length; i++) {
            String[] str = report[i].split(" ");
            String reporter = str[0];
            String reported = str[1];
            //이미 신고한적이 있다면 카운팅 안한다.
            if (!info.get(reporter).reportList.contains(reported)){
                info.get(reporter).report(reported);
                reportCounting.put(reported, reportCounting.get(reported)+1);
            }
        }
    }

}
728x90
반응형
LIST