문제: https://programmers.co.kr/learn/courses/30/lessons/92334
코딩테스트 연습 - 신고 결과 받기
문제 설명 신입사원 무지는 게시판 불량 이용자를 신고하고 처리 결과를 메일로 발송하는 시스템을 개발하려 합니다. 무지가 개발하려는 시스템은 다음과 같습니다. 각 유저는 한 번에 한 명의
programmers.co.kr
신고당한사람: [신고자] 딕셔너리를 만들어 신고당한 카운트를 세 주었습니다.
또 신고당한개수가 k개를 넘으면 그 사람을 신고한 사람에게 메일이 가도록 mail 딕셔너리를 만들었습니다.
조금 비효율적인 코드라고 생각하지만 효율성이 빡빡하지 않아서 통과한 것 같습니다.
import Foundation
func solution(_ id_list:[String], _ report:[String], _ k:Int) -> [Int] {
let report = Array(Set(report))
var result = [Int]()
var reported = [String: [String]]()
var mail = [String: Int]()
for repo in report {
let report = repo.components(separatedBy: " ")
let reporter = report[0] // 신고자
let reportee = report[1] // 당한사람
if reported[reportee] == nil {
reported[reportee] = [reporter]
} else {
reported[reportee]!.append(reporter)
}
}
for key in reported.keys {
if reported[key]!.count >= k { // 신고당했으면
for person in reported[key]! {
if mail[person] == nil {
mail[person] = 1
} else {
mail[person]! += 1
}
}
}
}
for person in id_list {
if mail[person] == nil {
result.append(0)
} else {
result.append(mail[person]!)
}
}
return result
}
solution(["muzi", "frodo", "apeach", "neo"], ["muzi frodo","apeach frodo","frodo neo","muzi neo","apeach muzi"], 2) // [2,1,1,0]
solution(["con", "ryan"], ["ryan con", "ryan con", "ryan con", "ryan con"], 3) // [0, 0]
solution([], [], 3) // [0, 0]
'알고리즘' 카테고리의 다른 글
[Swift] 프로그래머스 - 카카오 크레인 인형뽑기 게임 (0) | 2022.02.10 |
---|---|
[Swift] 프로그래머스 - 카카오 오픈채팅방 (0) | 2022.02.10 |
[Swift] 프로그래머스 - 로또의 최고 순위와 최저 순위 (0) | 2022.02.10 |
[Swift] 프로그래머스 - 카카오 신규 아이디 추천 (0) | 2022.02.10 |
[Swift] 프로그래머스 - 이중우선순위큐 (0) | 2022.02.09 |
댓글