나의 풀이
using System;
using System.Linq;
public class Solution {
public int[] solution(string[] id_list, string[] report, int k)
{
string[] distArray = report.Distinct().ToArray();
string[] resultArray = new string[distArray.Length];
for(int i = 0; i < distArray.Length; i++)
{
string str = distArray[i];
int idx = str.IndexOf(" ");
str = str.Substring(idx + 1);
resultArray[i] = str;
}
int[] reportCount = new int[id_list.Length];
int[] answer = new int[id_list.Length];
for(int i = 0; i < id_list.Length; i++)
{
for(int j = 0; j < resultArray.Length; j++)
{
if(id_list[i] == resultArray[j])
reportCount[i]++;
}
}
for(int i = 0; i < id_list.Length; i++)
{
if(reportCount[i] >= k)
{
for(int h = 0; h < resultArray.Length; h++)
{
if(resultArray[h] == id_list[i])
{
string str = distArray[h];
int idx = str.IndexOf(" ");
str = str.Substring(0, idx);
for(int j = 0; j < id_list.Length; j++)
{
if(str == id_list[j])
answer[j]++;
}
}
}
}
}
return answer;
}
}
해석
string[] distArray = report.Distinct().ToArray();
Why > 한사람이 같은 사람을 여러번 신고 했을 경우 1번의 신고만 인정 된다.
How > Distinct() 메소드를 통해 배열의 중복된 데이터 제거
string[] resultArray = new string[distArray.Length];
for(int i = 0; i < distArray.Length; i++){
string str = distArray[i];
int idx = str.IndexOf(" ");
str = str.Substring(idx + 1);
resultArray[i] = str;
}
Why > 처음에 문제를 잘못 이해해서 해당 유저가 신고 받은 횟수를 출력하는 문제로 착각했다. 그래서 신고받은 횟수를 알기 위해 distArray 데이터에서 신고한 유저정보를 뺀 데이터를 새로운 배열 변수에 넣어줬다.
How > distArray의 데이터에서 " "이후의 데이터(신고당한 유저 id)만 부분문자열로 받아와서 resultArray에 넣어줬다.
int[] reportCount = new int[id_list.Length];
int[] answer = new int[id_list.Length];
for(int i = 0; i < id_list.Length; i++){
for(int j = 0; j < resultArray.Length; j++){
if(id_list[i] == resultArray[j])
reportCount[i]++;
}
}
Why > 해당 유저가 신고 받은 횟수를 얻기위해 해당 코드를 썼다.
How > id_list 의 유저id가 resultArray의 데이터에 있을때 reportCount 배열, 같은 인덱스의 데이터에 횟수 추가
for(int i = 0; i < id_list.Length; i++)
{
if(reportCount[i] >= k)
{
for(int h = 0; h < resultArray.Length; h++)
{
if(resultArray[h] == id_list[i])
{
string str = distArray[h];
int idx = str.IndexOf(" ");
str = str.Substring(0, idx);
for(int j = 0; j < id_list.Length; j++)
{
if(str == id_list[j])
answer[j]++;
}
}
}
}
}
Why > 문제의도를 파악하고 유저가 받은 결과 메일 수를 얻기 위해 코드를 썼다.
How > distArray 에서 정지가 된 유저를 신고한 유저를 받아와서 결과 이메일을 받은 횟수에 추가
뭔가 쓸데 없는 변수가 있는거 같고 코드가 복잡해짐
다시 작성
using System;
using System.Linq;
public class Solution {
public int[] solution(string[] id_list, string[] report, int k)
{
int[] reportCount = new int[id_list.Length]; // 신고당한 횟수
int[] answer = new int[id_list.Length]; // 결과 메일 받은 횟수
report = report.Distinct().ToArray();
for(int i = 0; i < report.Length; i++)
{
string str = report[i];
int idx = str.IndexOf(" ");
str = str.Substring(idx + 1); // 신고 당한 유저
int userIdx = Array.IndexOf(id_list, str);
reportCount[userIdx]++; // 신고 당한 횟수 추가
}
for(int i = 0; i < report.Length; i++)
{
string data = report[i];
int idx = data.IndexOf(" ");
string reportUser = data.Substring(idx + 1);
int userIdx = Array.IndexOf(id_list, reportUser); // 신고 당한 유저
if(reportCount[userIdx] >= k)
{
string receiveUser = data.Substring(0, idx);
userIdx = Array.IndexOf(id_list, receiveUser); // 결과 메일 받은 유저
answer[userIdx]++;
}
}
return answer;
}
}
> report와 비슷한 데이터를 다루던 resultArray와 distArray 제거
> 신고 당한 횟수와 중복값을 제거한 report함수를 통해 문제풀이 진행
'코딩 테스트 > 프로그래머스' 카테고리의 다른 글
[프로그래머스][C# 풀이][Lv.0] 로그인 성공? (0) | 2023.11.16 |
---|---|
[프로그래머스][C# 풀이][Lv.0] 최빈값 구하기 (0) | 2023.11.16 |