swift 신고 결과 받기

quokka·2022년 5월 16일
0

코딩테스트

목록 보기
41/63
func solution(_ id_list:[String], _ report:[String], _ k:Int) -> [Int] {
    let report = Set(report).map { String($0) } // 중북제거
    var dict = [String: [String]]()
    var result = [Int]()
    // 신고당한사람, [신고한사람]
    for string in report {
        let stringArray = string.split(separator: " ").map { String($0) }
        let reporter = stringArray.first!
        let reported = stringArray.last!
        
        if dict[reported] == nil {
            dict.updateValue([reporter], forKey: reported)
        } else {
            var reporters = dict[reported]!
            reporters.append(reporter)
            dict.updateValue(reporters, forKey: reported)
        }
    }
    
    let resultDict = Dictionary(dict.filter { $0.value.count >= k }.values.flatMap { $0 }.map { ($0, 1) }, uniquingKeysWith: +)
    
    for i in id_list {
        if let num = resultDict[i] {
            result.append(num)
        } else {
            result.append(0)
        }
        
    }
    return result
}
profile
iOS를 공부하는 개발자입니다~ㅎㅎ

0개의 댓글

관련 채용 정보