Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 백준 17070번
- 2870번 수학숙제
- 2870번 c++
- C#
- 프로그래머스
- 17070번
- 백준 17070번 c++
- 유니티
- 백준
- 백준 c++ 2870번
- 백준 1103번 c++
- 2468 c++
- 코딩테스트
- 백준 1103번
- 백준 1103번 게임
- 수학숙제
- 백준 2870번
- Algorithm
- 코테
- c++
- Unity
- 2870번 수학숙제 c++
- dfs
- 2870번
- 오브젝트 풀링
- 플레이어 이동
- Lv.3
- Lv2
- Beakjoon
- 백준 c++ 2468번
Archives
- Today
- Total
주녘공부일지
[프로그래머스 C#] Lv.1 신고 결과 받기 본문
https://school.programmers.co.kr/learn/courses/30/lessons/92334
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
1. 정답코드 및 핵심 아이디어, 유의사항
신고자와 신고 대상에 대한 정보를 취합해 나온 정지 대상에 대해 기여한(?) 횟수를 구하는 문제
- 신고 정보 중에 중복되는 정보는 제외해야 함 (신고자와 신고 대상이 같을 경우)
-> bool타입 2차원 배열을 선언해 신고했다면 true로 바꿈으로써 중복되는 정보를 무시
주석 참조
using System;
using System.Collections.Generic;
public class Solution
{
public int[] solution(string[] id_list, string[] report, int k)
{
int length = id_list.Length;
int[] answer = new int[length];
var dict = new Dictionary<string, int>(); // <이름, 아이디>
var boolArrays = new bool[length, length]; // [신고자, 신고대상]
// 아이디 사전 세팅
for (int i = 0; i < length; i++)
dict.Add(id_list[i], i);
// 신고 처리
for (int i = 0; i < report.Length; i++)
{
string[] strs = report[i].Split(' '); // [신고자, 신고대상]
boolArrays[dict[strs[0]], dict[strs[1]]] = true; // 신고처리
}
// 신고 내용 취합 및 정지 처리
for (int i = 0; i < length; i++) // i : 신고대상
{
int count = 0; // 누적 신고 개수
for (int j = 0; j < length; j++) // j : 신고자
if (boolArrays[j, i])
count++;
// 정지 대상이라면 신고자들에게 메일을 보냄
if (count >= k)
for (int j = 0; j < length; j++)
if (boolArrays[j, i])
answer[j]++;
}
return answer;
}
}
+ System.Linq 네임스페이스를 적극 활용한 풀이
using System;
using System.Linq;
public class Solution
{
public int[] solution(string[] id_list, string[] report, int k)
{
var tReport = report.Distinct().
Select(s => s.Split(' ')).
GroupBy(g => g[1]).
Where(w => w.Count() >= k).
SelectMany(sm => sm.Select(s => s[0])).
ToList();
return id_list.ToDictionary(x => x, x => tReport.Count(c => x == c)).Values.ToArray();
}
}
'CodingTest > Programmers Lv.1' 카테고리의 다른 글
[프로그래머스 C#] Lv.1 동영상 재생기 (PCCP 기출문제 1번) (0) | 2024.09.22 |
---|---|
[프로그래머스 C++] Lv.1 완주하지 못한 선수 (0) | 2024.07.30 |
[프로그래머스 C#] Lv.1 개인정보 수집 유효기간 (0) | 2024.02.13 |
[프로그래머스 C#] Lv.1 옹알이(2) (0) | 2024.01.15 |
[프로그래머스 C#] Lv.1 가장 많이 받은 선물 (0) | 2024.01.04 |