Archive/Questions
[programmers/Swift] 신고 결과 받기 (2022 Kakao Blind Recruitment)
Marco
2022. 8. 4. 15:38
728x90
programmers.co.kr - 코딩테스트연습 - Lv.1 - 2022 Kakao Blind Recruitment - 신고 결과 받기
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
Constraints :
- 2 ≤ id_list의 길이 ≤ 1,000
//
// Created by Yongwoo Marco on 2022/08/04.
// Copyright © 2022 Yongwoo Marco Kim. All rights reserved.
//
func solution(_ id_list:[String], _ report:[String], _ k:Int) -> [Int] {
var reportedCount = [String: Int]()
var table = [String: Set<String>]()
Set(report).forEach {
let splits = $0.split(separator: " ").map { String($0) }
let (reporter, reportedUser) = (splits[0], splits[1])
if table[reporter] == nil {
table.updateValue(Set([reportedUser]), forKey: reporter)
} else {
table[reporter]?.update(with: reportedUser)
}
reportedCount[reportedUser] = (reportedCount[reportedUser] ?? 0) + 1
}
return id_list.map { user in
return (table[user] ?? []).reduce(0) {
$0 + ((reportedCount[$1] ?? 0) >= k ? 1 : 0)
}
}
}
print(solution(["muzi", "frodo", "apeach", "neo"],
["muzi frodo","apeach frodo","frodo neo","muzi neo","apeach muzi"],
2)) // [2,1,1,0]
print(solution(["con", "ryan"],
["ryan con", "ryan con", "ryan con", "ryan con"],
3)) // [0,0]
How I tried this :
처음엔 시간초과가 나왔다.
생각해보니 동일한 신고는 무조건 1회로 처리되기 때문에 'report' 배열을 Set으로 처리해서 중복을 없앴다.
그러니 다행히 통과됨
What I got is :
다른 사람의 풀이를 보고 편리한 딕셔너리 접근법을 알게 되었다.
reportedCount[reported] = (reportedCount[reported] ?? 0) + 1
// 값이 없다면 default 값을 넣어주는 형태로 배열처럼 접근이 가능하다..
문제에 관한 모든 저작권 : https://programmers.co.kr/
728x90