본문 바로가기
Algorithm

SwiftUI: 가장 많이 받은 선물

by songmoro 2024. 2. 5.
728x90
func solution(_ friends:[String], _ gifts:[String]) -> Int {
    // 2차원 테이블 생성
    //   a b c 합
    // a 0 0 0 0
    // b 0 0 0 0
    // c 0 0 0 0
    var table = Array(repeating: Array(repeating: 0, count: friends.count + 1), count: friends.count)
    
    // 인덱싱 용 딕셔너리
    var index: [String: Int] = [:]
    
    // 인덱싱 매핑
    _ = friends.reduce(0) { partialResult, friend in
        index[friend] = partialResult
        
        return partialResult + 1
    }
    
    // 테이블 매핑
    gifts.map {
        let i = index[String($0.split(separator: " ").first!)]!
        let j = index[String($0.split(separator: " ").last!)]!
        
        table[i][j] += 1
        table[i][friends.count] += 1
        table[j][friends.count] -= 1
    }
    
    // 최대 선물 개수 반환
    return friends.reduce(0) { partialResult, friend in
        let i = index[friend]!
        
        var count = 0
        for j in 0..<friends.count {
            if i == j {
                continue
            }
            
            if table[i][j] > table[j][i] {
                count += 1
            }
            else if table[i][j] == table[j][i], table[i][friends.count] > table[j][friends.count] {
                count += 1
            }
        }
        
        return max(partialResult, count)
    }
}
728x90

'Algorithm' 카테고리의 다른 글

SwiftUI: 숫자 짝꿍  (0) 2024.03.07
SwiftUI: 체육복  (0) 2024.03.07
SwiftUI: 크레인 인형뽑기 게임  (1) 2023.12.18
SwiftUI: [카카오 인턴] 키패드 누르기  (1) 2023.12.18
SwiftUI: 햄버거 만들기  (0) 2023.12.17