본문 바로가기
개발/알고리즘

[프로그래머스/JAVA] 신고 결과 받기

by zuzuu 2022. 7. 24.
반응형



👇 문제 URL 👇

 

프로그래머스

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

programmers.co.kr

 

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

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

    // HashMap : Key-value, hashSet: set 중 성능이 가장 좋음
    // key : 신고된 ID, value : 신고한 ID set
    Map<String, HashSet<String>> reportMap = new HashMap<>();

    // key : 신고한 ID, value : 메일 받는 횟수
    Map<String, Integer> resultMap = new HashMap<>();

    for (int i = 0; i < id_list.length; i++) {
        String name = id_list[i];
        reportMap.put(name, new HashSet<>());
        resultMap.put(name, 0);
    }

    for(String user_report : report){
        String[] reportArr = user_report.split(" ");
        reportMap.get(reportArr[1]).add(reportArr[0]); //신고한 ID를 HashSet에 추가해줌
    }

    //reportMap 데이터 출력
    /*reportMap.forEach((key, value) -> {
        System.out.print(key +":" );
        value.forEach((str)->System.out.print(str +"," ));
        System.out.println();
    } );*/

    //신고한 ID별 메일 받는 횟수 추가
    reportMap.forEach((key,value) -> {

        //신고당한 횟수가 k회 이상이라면 메일 발송
        if(value.size() >= k){

            value.forEach((str) -> {

                //신고자명 찾아서 메일 횟수 추가
                int cnt = resultMap.get(str).intValue() +1;
                resultMap.replace(str, cnt);

            });
        }
    });

    //resultMap 데이터 출력
    /*resultMap.forEach((key, value) -> {
        System.out.println(key +":" + value);
    } );*/


    for(int i=0; i< id_list.length; i++){
        answer[i] = resultMap.get(id_list[i]).intValue();
    }

    return answer;
}


테스트도 다 통과했다!



728x90
반응형

댓글