๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

2022 KAKAO ๋ธ”๋ผ์ธ๋“œ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ - ์‹ ๊ณ  ๊ฒฐ๊ณผ ๋ฐ›๊ธฐ

by yunamom 2022. 4. 1.
๋ฐ˜์‘ํ˜•

2022 KAKAO ๋ธ”๋ผ์ธ๋“œ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ - ์‹ ๊ณ  ๊ฒฐ๊ณผ ๋ฐ›๊ธฐ

๐Ÿ’ก๋ฌธ์ œ ์„ค๋ช…

์‹ ์ž…์‚ฌ์› ๋ฌด์ง€๋Š” ๊ฒŒ์‹œํŒ ๋ถˆ๋Ÿ‰ ์ด์šฉ์ž๋ฅผ ์‹ ๊ณ ํ•˜๊ณ  ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ๋ฉ”์ผ๋กœ ๋ฐœ์†กํ•˜๋Š” ์‹œ์Šคํ…œ์„ ๊ฐœ๋ฐœํ•˜๋ ค ํ•ฉ๋‹ˆ๋‹ค.
๋ฌด์ง€๊ฐ€ ๊ฐœ๋ฐœํ•˜๋ ค๋Š” ์‹œ์Šคํ…œ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.
  • ๊ฐ ์œ ์ €๋Š” ํ•œ ๋ฒˆ์— ํ•œ ๋ช…์˜ ์œ ์ €๋ฅผ ์‹ ๊ณ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    • ์‹ ๊ณ  ํšŸ์ˆ˜์— ์ œํ•œ์€ ์—†์Šต๋‹ˆ๋‹ค. ์„œ๋กœ ๋‹ค๋ฅธ ์œ ์ €๋ฅผ ๊ณ„์†ํ•ด์„œ ์‹ ๊ณ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    • ํ•œ ์œ ์ €๋ฅผ ์—ฌ๋Ÿฌ ๋ฒˆ ์‹ ๊ณ ํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ๋™์ผํ•œ ์œ ์ €์— ๋Œ€ํ•œ ์‹ ๊ณ  ํšŸ์ˆ˜๋Š” 1ํšŒ๋กœ ์ฒ˜๋ฆฌ๋ฉ๋‹ˆ๋‹ค.
  • k๋ฒˆ ์ด์ƒ ์‹ ๊ณ ๋œ ์œ ์ €๋Š” ๊ฒŒ์‹œํŒ ์ด์šฉ์ด ์ •์ง€๋˜๋ฉฐ, ํ•ด๋‹น ์œ ์ €๋ฅผ ์‹ ๊ณ ํ•œ ๋ชจ๋“  ์œ ์ €์—๊ฒŒ ์ •์ง€ ์‚ฌ์‹ค์„ ๋ฉ”์ผ๋กœ ๋ฐœ์†กํ•ฉ๋‹ˆ๋‹ค.
    • ์œ ์ €๊ฐ€ ์‹ ๊ณ ํ•œ ๋ชจ๋“  ๋‚ด์šฉ์„ ์ทจํ•ฉํ•˜์—ฌ ๋งˆ์ง€๋ง‰์— ํ•œ๊บผ๋ฒˆ์— ๊ฒŒ์‹œํŒ ์ด์šฉ ์ •์ง€๋ฅผ ์‹œํ‚ค๋ฉด์„œ ์ •์ง€ ๋ฉ”์ผ์„ ๋ฐœ์†กํ•ฉ๋‹ˆ๋‹ค.
๋‹ค์Œ์€ ์ „์ฒด ์œ ์ € ๋ชฉ๋ก์ด ["muzi", "frodo", "apeach", "neo"]์ด๊ณ ,
k = 2(์ฆ‰, 2๋ฒˆ ์ด์ƒ ์‹ ๊ณ ๋‹นํ•˜๋ฉด ์ด์šฉ ์ •์ง€)์ธ ๊ฒฝ์šฐ์˜ ์˜ˆ์‹œ์ž…๋‹ˆ๋‹ค.
์œ ์ € ID ์œ ์ €๊ฐ€ ์‹ ๊ณ ํ•œ ID ์„ค๋ช…
"muzi" "frodo" "muzi"๊ฐ€ "frodo"๋ฅผ ์‹ ๊ณ ํ–ˆ์Šต๋‹ˆ๋‹ค.
"apeach" "frodo" "apeach"๊ฐ€ "frodo"๋ฅผ ์‹ ๊ณ ํ–ˆ์Šต๋‹ˆ๋‹ค.
"frodo" "neo" "frodo"๊ฐ€ "neo"๋ฅผ ์‹ ๊ณ ํ–ˆ์Šต๋‹ˆ๋‹ค.

 

๐Ÿ‘‡๊ฐ ์œ ์ €๋ณ„๋กœ ์‹ ๊ณ ๋‹นํ•œ ํšŸ์ˆ˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์œ ์ € ID ์‹ ๊ณ ๋‹นํ•œ ํšŸ์ˆ˜
"Muzi" 1
"frodo" 2
"apeach" 0
"neo" 2

 

์œ„ ์˜ˆ์‹œ์—์„œ๋Š” 2๋ฒˆ ์ด์ƒ ์‹ ๊ณ ๋‹นํ•œ "frodo"์™€ "neo"์˜ ๊ฒŒ์‹œํŒ ์ด์šฉ์ด ์ •์ง€๋ฉ๋‹ˆ๋‹ค.

์ด๋•Œ, ๊ฐ ์œ ์ €๋ณ„๋กœ ์‹ ๊ณ ํ•œ ์•„์ด๋””์™€ ์ •์ง€๋œ ์•„์ด๋””๋ฅผ ์ •๋ฆฌํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์œ ์ € ID ์œ ์ €๊ฐ€ ์‹ ๊ณ ํ•œ ID ์ •์ง€๋œ ID
"muzi" ["frodo", "neo"] ["frodo", "neo"]
"frodo" ["neo"] ["neo"]
"apeach" ["muzi", "frodo"] ["frodo"]
"neo" ์—†์Œ ์—†์Œ

 

๋”ฐ๋ผ์„œ "muzi"๋Š” ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ ๋ฉ”์ผ์„ 2ํšŒ, "frodo"์™€ "apeach"๋Š” ๊ฐ๊ฐ ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ ๋ฉ”์ผ์„ 1ํšŒ ๋ฐ›๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

์ด์šฉ์ž์˜ ID๊ฐ€ ๋‹ด๊ธด ๋ฌธ์ž์—ด ๋ฐฐ์—ด id_list,

๊ฐ ์ด์šฉ์ž๊ฐ€ ์‹ ๊ณ ํ•œ ์ด์šฉ์ž์˜ ID ์ •๋ณด๊ฐ€ ๋‹ด๊ธด ๋ฌธ์ž์—ด ๋ฐฐ์—ด report,

์ •์ง€ ๊ธฐ์ค€์ด ๋˜๋Š” ์‹ ๊ณ  ํšŸ์ˆ˜ k๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ,

๊ฐ ์œ ์ €๋ณ„๋กœ ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ ๋ฉ”์ผ์„ ๋ฐ›์€ ํšŸ์ˆ˜๋ฅผ ๋ฐฐ์—ด์— ๋‹ด์•„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.


์ž…์ถœ๋ ฅ ์˜ˆ
id_list report k result
["muzi", "frodo", "apeach", "neo"] ["muzi frodo","apeach frodo","frodo neo","muzi neo","apeach muzi"] 2 [2,1,1,0]
["con", "ryan"] ["ryan con", "ryan con", "ryan con", "ryan con"] 3 [0,0]
์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

์ž…์ถœ๋ ฅ ์˜ˆ #1

๋ฌธ์ œ์˜ ์˜ˆ์‹œ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ #2

"ryan"์ด "con"์„ 4๋ฒˆ ์‹ ๊ณ ํ–ˆ์œผ๋‚˜, ์ฃผ์–ด์ง„ ์กฐ๊ฑด์— ๋”ฐ๋ผ ํ•œ ์œ ์ €๊ฐ€ ๊ฐ™์€ ์œ ์ €๋ฅผ ์—ฌ๋Ÿฌ ๋ฒˆ ์‹ ๊ณ ํ•œ ๊ฒฝ์šฐ๋Š” ์‹ ๊ณ  ํšŸ์ˆ˜ 1ํšŒ๋กœ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ "con"์€ 1ํšŒ ์‹ ๊ณ ๋‹นํ–ˆ์Šต๋‹ˆ๋‹ค.

3๋ฒˆ ์ด์ƒ ์‹ ๊ณ ๋‹นํ•œ ์ด์šฉ์ž๋Š” ์—†์œผ๋ฉฐ, "con"๊ณผ "ryan"์€ ๊ฒฐ๊ณผ ๋ฉ”์ผ์„ ๋ฐ›์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ [0, 0]์„ return ํ•ฉ๋‹ˆ๋‹ค.

 

package ์‹ ๊ณ ๊ฒฐ๊ณผ๋ฐ›๊ธฐ;

import java.util.*;

class Solution {
    public int[] solution(String[] id_list, String[] report, int k) {
        int[] answer = new int[id_list.length];
        
        Map<String, Integer> index = new HashMap<>();
        Map<String, List<Integer>> list = new HashMap<>();
        
        for(int i=0; i<id_list.length; i++) {
        
        	index.put(id_list[i], i);
        }
        
        for(String rep : report) {
        	/* ๊ณต๋ฐฑ์„ ๊ธฐ์ค€์œผ๋กœ ๋‚˜๋ˆ„์–ด์ค€๋‹ค. */
        	String[] id = rep.split(" ");
        	
        	/* ๋ณธ์ธ์ด ์‹ ๊ณ ํ•œ๊ฒฝ์šฐ ์ œ์™ธ */
        	if(!list.containsKey(id[1])) {
        		list.put(id[1], new ArrayList<>());
        	}
        	List<Integer> temp = list.get(id[1]);
        	/* ์ค‘๋ณต ์‹ ๊ณ  ์ œ์™ธ */
        	if(!temp.contains(index.get(id[0]))) {
        		temp.add(index.get(id[0]));
        	}
        }
        for(String id : id_list) {
        	if(list.containsKey(id) && list.get(id).size() >= k) {
        		for(int idx : list.get(id)) {			
        			answer[idx]++;
        		}		
        	}
        }
        return answer;
    }
}
public class Algorithm {
	
	public static void main(String[] args) {
		
		Solution test = new Solution();
		/* test code ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ž…๋‹ˆ๋‹ค. */
		String[] id_list = {
				"muzi",
				"frodo",
				"apeach",
				"neo"};
		
		String[] report = {
				"muzi frodo",
				"apeach frodo",
				"frodo neo",
				"muzi neo",
				"apeach muzi"};
		
		System.out.println(Arrays.toString(test.solution(id_list, report, 2)));
	}
}
/* ๊ฒฐ๊ณผ -> [2, 1, 1, 0] */

 

 

300x250

์ฝ”๋“œ