C# 신고 결과 받기

2022. 3. 22. 13:45C#/[프로그래머스] 카카오 문제

문제

코드 :

using System;
public class Solution
{
    public static void Reset2(int[,] array) //초기화
        {
            for (int i = 0; i < array.GetLength(0); i++)
            {
                for (int j = 0; j < array.GetLength(1); j++)
                {
                    array[i, j] = 0;
                }
            }
        }
    public static void Reset(int[] array) //초기화
        {
            for (int i = 0; i < array.Length; i++)
            {
                array[i] = 0;
            }
        }
    
   public int[] solution(string[] id_list, string[] report, int k)
    {
        int[] answer = new int[id_list.Length];
        int[,] reportArray = new int[id_list.Length, id_list.Length];
        int[] countArray = new int[id_list.Length];            

        Reset2(reportArray);
        Reset(answer);
        Reset(countArray);

        for (int i = 0; i < report.Length; i++)
        {
            string[]reports = report[i].Split(' ');
            //신고자
            string firstRts = reports[0];
            //신고당한자
            string secondRts = reports[1];

            int first = 0;
            int second = 0;

            //신고자와 신고당한자의 번호를 알기 위한 과정
            for (int j = 0; j < id_list.Length; j++)
            {
                if (id_list[j] == firstRts)
                    first = j;
                if (id_list[j] == secondRts)
                    second = j;
            }
            //같은 신고이력이 없으면
            if(reportArray[first, second] == 0)
            {
                //신고했음을 표기
                reportArray[first, second] = 1;
                //신고당한자 횟수 증가
                countArray[second] += 1;
            }
        }
        for (int i = 0; i < reportArray.GetLength(0); i++)
        {
            for (int j = 0; j < reportArray.GetLength(1); j++)
            {
                if (reportArray[i, j] == 1 && countArray[j] >= k)
                {
                    answer[i] += 1;
                }
            }
        }
        return answer;
    }
}

문제풀이 :

1. 출력 배열, 2차원 배열을 만들어서 누가 누구를 신고했는지를 체크할 수 있는 배열, 신고당한자가 몇번 당했는지 기록할 수 있는 배열을 만든다.

2. 2차원 배열을 통해 중복 신고를 방지하고 k보다 높은 신고를 당한자는 출력배열에서 +1 한다.

 

'C# > [프로그래머스] 카카오 문제' 카테고리의 다른 글

C# 숫자 문자열과 영단어  (0) 2022.03.22