Geon

신고 결과 받기 본문

코딩테스트

신고 결과 받기

jgkim1008 2022. 3. 18. 08:54

코드


import Foundation

func solution(_ id_list:[String], _ report:[String], _ k:Int) -> [Int] {
    var dict: [String: [String]] = [:]
    var result = Array(repeating: 0, count: id_list.count)
    var countDict: [String:Int] = [:]

    for (index, id) in id_list.enumerated() {
        countDict[id] = index
    }

    for repo in report {
        let arr = repo.split(separator: " ").compactMap {String($0)}
        let beAccused = arr[1]
        let accusation = arr[0]
        if dict[beAccused] == nil {
            dict.updateValue([accusation], forKey: beAccused)
        } else {
            if !dict[beAccused]!.contains(accusation) {
                dict[beAccused]!.append(accusation)
            }
        }
    }

    for i in dict.keys {
        if dict[i]!.count >= k {
            for n in dict[i]! {
                    result[countDict[n]!] += 1
            }            
        }
    }

    return result

}

배운점

  • Array(repeating:,count:) 를 통해 배열안에 있는 요소의 갯수를 지정 가능
  • 딕셔너리의 인덱스를 지정할수 있다.

'코딩테스트' 카테고리의 다른 글

단어뒤집기[BOJ]  (0) 2022.03.22
스택[BOJ]  (0) 2022.03.22
로또의 최고 순위와 최저 순위  (0) 2022.03.16
신규 아이디 추천  (0) 2022.03.15
숫자 문자열과 영단어  (0) 2022.03.15