부트캠프/Unity [ 스파르타 ]

[스파르타 ][TIL] 유니티 스파르타 7기 53일차 - 신고 결과 받기

맏난거 2025. 4. 24. 22:52

using System;
using System.Linq;
using System.Collections.Generic;

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

        // 실질적으로 필요한 메모리공간
        int[] reportedCount = new int[id_list.Length];
        Dictionary<string, int> dict = new Dictionary<string, int>();
        Dictionary<string, int> keys = new Dictionary<string, int>();

        // 키값저장
        for(int i =0; i<id_list.Length; i++)
            keys.Add(id_list[i], i);

        // 중복제거
        for(int i = 0; i < report.Length; i++)
        {
            if (dict.TryGetValue(report[i], out var Dump) == false)
                dict.Add(report[i], i);
        }

        foreach(var item in dict)
        {
            // 인덱스 0 : 신고한 사람
            // 인덱스 1 : 신고당한 사람
            // int reportedKey = keys[result[1]] : int 타입 키

            string[] result = CustomSplit(item.Key);
            int reportedKey = keys[result[1]];
            reportedCount[reportedKey] += 1;
        }

        // 결과 확인
        foreach(var item in dict)
        {
            // 인덱스 0 : 신고한 사람
            // 인덱스 1 : 신고당한 사람
            // int cleanerKey = keys[result[0]] : int 타입 키
            // int reportedKey = keys[result[1]] : int 타입 키

            string[] result = CustomSplit(item.Key);
            int cleanerKey = keys[result[0]];
            int reportedKey = keys[result[1]];

            if(reportedCount[reportedKey] >= k)
                answer[cleanerKey]++;
        }

        return answer;
    }


    public string[] CustomSplit(string str)
    {
        return str.Split(" ");
    }
}

 

문제 접근한 방법:

- 중복제거를 해야되서 딕셔너리를 가장 먼저 떠올렸고 중복제거를 진행

- 배열을 만들고 바로바로 접근할 수 있도록 키값을 저장해주는 하나 더 딕셔너리를 생성

 

1. 중복제거 방법

딕셔너리는 하나의 키값만 가지고 있을 수 있기때문에 이걸 이용해서 중복 제거를 했습니다.

저기에 나와있는 문자열 자체가 키로 생각하면 편합니다.

muzi frodo가 처음에 나왔으면 다음으로 나오는 muzi frodo는 무시

 

2. 키값지정

keys라는 딕셔너리에 고유의 이름을 집어넣고 반복문을 돌려서 고유의 번호 느낌처럼 넣어줬습니다.

이 두개를 이용해서 foreach문으로 편하게 돌 수 있습니다.

3. 신고 카운트

본격적으로 Split으로 자르고

신고당한사람의 고유의 키(인덱스)를 가져와서

신고 당한 횟수배열에 접근해서 카운트를 합니다.

 

4. 결과 확인

 

똑같이 밑에서 foreach문을 돌고

이번에는 신고한 사람의 고유의 키와 신고를 당한 사람의 고유의 키를 가져와서

신고카운트배열의 값이 k번을 넘기면

신고했던 사람의 고유의 키로 접근해서 카운트를 해줍니다.

- O(n)