-
Notifications
You must be signed in to change notification settings - Fork 0
Closed
Description
💬 문제
[코딩테스트 연습 - 신고 결과 받기](https://school.programmers.co.kr/learn/courses/30/lessons/92334)
💬 Idea
- 신고 기록을 담는 [String: [String]]형 딕셔너리, 신고된 기록을 담는 [String: Int]형 딕셔너리를 만들어 id_report를 돌며 해당 딕셔너리를 채운다.
- 이 때, 한 유저를 여러 번 신고할 수도 있지만, 동일한 유저에 대한 신고 횟수는 1회로 처리됩니다. 라는 조건을 만족하기 위해 for문을 돌 때 Set처리를 해준다.
- 결과를 도출한다.
- 딕셔너리는 순서가 없는데, return해야하는 결과값은 id_list의 순서와 같아야 하므로
- id_list를 돌며
- report_list가 존재하는 id의 경우 해당 신고 기록들에서 k번 이상의 신고를 받은 유저의 count를 필터링하여 append하고
- report_list가 존재하지 않는 id의 경우 0을 append 하여 최종 결과를 도출한다.
💬 풀이
func solution(_ id_list:[String], _ report:[String], _ k:Int) -> [Int] {
var report_list: [String: [String]] = [:]
var reported_list: [String: Int] = [:]
var result: [Int] = []
for id_report in Set(report) {
let list = id_report.components(separatedBy: .whitespaces)
report_list[list[0]] = report_list[list[0]] != nil ? report_list[list[0]]! + [list[1]] : [list[1]]
reported_list[list[1]] = reported_list[list[1]] != nil ? reported_list[list[1]]! + 1 : 1
}
// ✅ report_list
// ["apeach": ["muzi", "frodo"], "frodo": ["neo"], "muzi": ["neo", "frodo"]]
// ✅ reported_list
// ["neo": 2, "frodo": 2, "muzi": 1]
id_list.forEach {
result.append(report_list[$0] != nil ? (report_list[$0]?.filter({ reported_list[$0]! >= k }).count)! : 0)
}
return result
}