๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ๋ฐœ/์•Œ๊ณ ๋ฆฌ์ฆ˜

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค/JAVA] ์‹ ๊ณ  ๊ฒฐ๊ณผ ๋ฐ›๊ธฐ

by ynzu๐Ÿค 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
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€