[Swift - ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์ˆœ์œ„

2021. 6. 10. 15:07ใ†๐Ÿง‘๐Ÿป‍๐Ÿ’ป iOS ๊ฐœ๋ฐœ/Swift - Algorithm - Solutions


๐Ÿค”  ํ’€์ด ๊ณผ์ •

n๋ช…์˜ ๊ถŒํˆฌ์„ ์ˆ˜๊ฐ€ ๋“ฑ์žฅํ•˜๊ณ  1๋ถ€ํ„ฐ n๊นŒ์ง€ ๋ฒˆํ˜ธ๋ฅผ ๋งค๊ธฐ๋Š”๋ฐ์š”. ๊ทธ๋“ค ๊ฐ„์˜ 1๋Œ€ 1 ๋งค์นญ ๊ฒฐ๊ณผ๊ฐ€ results๋ฐฐ์—ด์„ ํ†ตํ•ด ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ๊ฐ๊ฐ์˜ ๋ฐฐ์—ด์—๋Š” ์Šน์ž์™€ ํŒจ์ž์˜ ์›์†Œ๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ๋‹ด์€ ๋ฐฐ์—ด๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค. ์ด ์ •๋ณด๋ฅผ ๊ทธ๋ž˜ํ”„๋กœ ํ‘œํ˜„ํ•˜๊ณ  ๊ฐ๊ฐ์˜ ์„ ์ˆ˜๋งˆ๋‹ค DFS๋กœ ๊ทธ๋ž˜ํ”„ ํƒ์ƒ‰ํ•˜๋ฉฐ ์ž์‹ ๋ณด๋‹ค ๊ฐ•ํ•œ ์‚ฌ๋žŒ์˜ ์ˆ˜, ์ž์‹ ๋ณด๋‹ค ์•ฝํ•œ ์‚ฌ๋žŒ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•ด๋ณด๊ณ  ๊ทธ ํ•ฉ์ด n - 1์ด ๋˜๋ฉด ์ˆœ์œ„๋ฅผ ์•Œ ์ˆ˜ ์žˆ๋‹ค๊ณ  ํŒ๋‹จํ•  ์ˆ˜ ์žˆ๊ฒ ์Šต๋‹ˆ๋‹ค.

๋จผ์ € ๊ถŒํˆฌ ์„ ์ˆ˜์˜ ์ˆ˜๋งŒํผ ์ž์‹ ๋ณด๋‹ค ๊ฐ•ํ•œ ์‚ฌ๋žŒ์˜ ์ˆ˜์™€ ์ž์‹ ๋ณด๋‹ค ์•ฝํ•œ ์‚ฌ๋žŒ์˜ ์ˆ˜๋ฅผ ์ €์žฅํ•  ๋ฐฐ์—ด ๋‘ ๊ฐœ๋ฅผ ๋งŒ๋“ค์–ด์ฃผ๊ณ  ๊ฒฝ๊ธฐ๊ฒฐ๊ณผ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ ํ˜•ํƒœ๋กœ ๊ทธ๋ž˜ํ”„๋ฅผ ํ‘œํ˜„ํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.

์˜ค๋ Œ์ง€์ƒ‰์„ ๊ธฐ์ค€ ์„ ์ˆ˜๋ผ๊ณ  ํ•˜์˜€์„ ๋•Œ ๋ถ„ํ™์ƒ‰์€ ํ•ด๋‹น ์„ ์ˆ˜๋ณด๋‹ค ์•ฝํ•œ ์„ ์ˆ˜๋“ค์˜ ๋ฆฌ์ŠคํŠธ์ž…๋‹ˆ๋‹ค. ์ด์ œ ๊ฐ๊ฐ์˜ ์„ ์ˆ˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ DFSํƒ์ƒ‰์„ ํ•˜๊ฒŒ ๋˜๋ฉด ๋ฐฉ๋ฌธ๋œ ์„ ์ˆ˜๋Š” ๊ธฐ์ค€ ์„ ์ˆ˜๋ณด๋‹ค ์ž์‹ ์ด ์•ฝํ•˜๋‹ค๋Š” ๊ฒƒ์ด๋ฏ€๋กœ ๋ฐฉ๋ฌธ ๋˜์–ด์งˆ ๋•Œ๋งˆ๋‹ค ์ž์‹ ๋ณด๋‹ค ๊ฐ•ํ•œ ์‚ฌ๋žŒ์˜ ์ˆ˜๋ฅผ ํ•˜๋‚˜์”ฉ ๋Š˜๋ ค์ฃผ๊ณ  DFS ํ˜ธ์ถœ ํšŒ์ˆ˜๋Š” ๊ธฐ์ค€ ์‚ผ์•˜๋˜ ์„ ์ˆ˜๋ณด๋‹ค ์•ฝํ•œ ์„ ์ˆ˜๋“ค์˜ ์ˆ˜๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.  

์œ„์˜ ์„ค๋ช…๋Œ€๋กœ DFS ํƒ์ƒ‰์„ ํ•˜๋ฉฐ ๋ฐฐ์—ด์„ ๊ฐฑ์‹ ํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

๋งˆ์ง€๋ง‰์œผ๋กœ, ๋‘ ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค๋ผ๋ฆฌ์˜ ํ•ฉ์ด n - 1 ์ด ๋˜๋Š” ์ˆ˜๋ฅผ ์„ธ์–ด์ฃผ์–ด์„œ ๋‹ต์„ ๊ตฌํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค. 


๐Ÿ“ฆ  ๋ฌธ์ œ ํ’€์ด & ์ฝ”๋“œ

import Foundation

func solution(_ n:Int, _ results:[[Int]]) -> Int {
    var graph = [[Int]](repeating: [Int](), count: n + 1)
    var weakerThanMe = [Int](repeating: 0, count: n + 1)
    var strongerThanMe = [Int](repeating: 0, count: n + 1)
    var visited = [Bool](repeating: false, count: n + 1)
    var weakerCounter = 0
    
    func dfs(_ cur: Int) {
        visited[cur] = true
        
        for weaker in graph[cur] {
            if visited[weaker] == false {
//                print(weaker, terminator: " ")
                strongerThanMe[weaker] += 1
                weakerCounter += 1
                dfs(weaker)
            }
        }
    }

    for result in results {
        let winner = result[0]
        let loser = result[1]
        
        graph[winner].append(loser)
//        graph[winner].sort(by: <)
    }
    
    for idx in 1...n {
        visited = [Bool](repeating: false, count: n + 1)
        weakerCounter = 0
//        print("\(idx)๋ฒˆ ์„ ์ˆ˜ ๋ณด๋‹ค ์•ฝํ•œ ์„ ์ˆ˜ ->", terminator: " ")
        dfs(idx)
//        print(" \(weakerCounter)๋ช…")
        weakerThanMe[idx] = weakerCounter
        
    }
    
    var answer = 0

    for idx in 1...n {
        if strongerThanMe[idx] + weakerThanMe[idx] == n - 1 {
            answer += 1
        }
    }
    
//    print()
//    strongerThanMe.removeFirst()
//    weakerThanMe.removeFirst()
//    print("์ž์‹ ๋ณด๋‹ค ๊ฐ•ํ•œ ์‚ฌ๋žŒ์˜ ์ˆ˜:", terminator: " ")
//    print(strongerThanMe)
//    print("์ž์‹ ๋ณด๋‹ค ์•ฝํ•œ ์‚ฌ๋žŒ์˜ ์ˆ˜:", terminator: " ")
//    print(weakerThanMe)
    
    return answer
}

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์ˆœ์œ„

5 [[4, 3], [4, 2], [3, 2], [1, 2], [2, 5]] 2

programmers.co.kr