[Swift - ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ํ•ฉ์Šน ํƒ์‹œ ์š”๊ธˆ

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

๐Ÿ•ต๐Ÿป  ๋ฌธ์ œ ํ•ด์„ 


๋ฌด์ง€์™€ ์–ดํ”ผ์น˜๋Š” ์ตœ๊ทผ ์žฆ์•„์ง„ ์•ผ๊ทผ ๋•Œ๋ฌธ์— ํƒ์‹œ๋ฅผ ํƒ€๊ณ  ๊ท€๊ฐ€ํ•˜๋Š” ์ผ์ด ๋Š˜์–ด๋‚ฌ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ ํƒ์‹œ๋ฅผ ๊ฐ™์ด ํƒ€๊ณ  ๊ท€๊ฐ€ํ•˜์—ฌ ์š”๊ธˆ์„ ์ตœ๋Œ€ํ•œ ์ค„์—ฌ๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ํ•จ๊ป˜ ์ถœ๋ฐœํ•˜๋Š” ์ง€์  s, ๊ทธ๋ฆฌ๊ณ  ๊ฐ์ž์˜ ์ง‘์ด ์œ„์น˜ํ•˜๋Š” a, b ์ง€์ ๊ณผ ์ง€์ ๊ณผ ์ง€์  ์‚ฌ์ด์˜ ์˜ˆ์ƒ์š”๊ธˆ์ด fare ๋ฐฐ์—ด๋กœ ์ฃผ์–ด์งˆ ๋•Œ ์ตœ์†Œ์š”๊ธˆ์„ ๊ณ„์‚ฐํ•ด๋ด…์‹œ๋‹ค.

[์ œํ•œ์‚ฌํ•ญ]

  • ์ง€์ ๊ฐฏ์ˆ˜ n์€ 3 ์ด์ƒ 200 ์ดํ•˜์ธ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.
  • ์ง€์  s, a, b๋Š” 1 ์ด์ƒ n ์ดํ•˜์ธ ์ž์—ฐ์ˆ˜์ด๋ฉฐ, ๊ฐ๊ธฐ ์„œ๋กœ ๋‹ค๋ฅธ ๊ฐ’์ž…๋‹ˆ๋‹ค.
  • ์ถœ๋ฐœ์ง€์ , A์˜ ๋„์ฐฉ์ง€์ , B์˜ ๋„์ฐฉ์ง€์ ์€ ์„œ๋กœ ๊ฒน์น˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • ์š”๊ธˆ f๋Š” 1 ์ด์ƒ 100,000 ์ดํ•˜์ธ ์ž์—ฐ์ˆ˜์ž…๋‹ˆ๋‹ค.
  • ์ถœ๋ฐœ์ง€์  s์—์„œ ๋„์ฐฉ์ง€์  a์™€ b๋กœ ๊ฐ€๋Š” ๊ฒฝ๋กœ๊ฐ€ ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ๋งŒ ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง‘๋‹ˆ๋‹ค.

 

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


๋จผ์ € ๋ฌธ์ œ๋ฅผ ์ฝ๊ณ  ๊ทธ๋ž˜ํ”„ ๋ฌธ์ œ๋ผ๋Š” ๊ฒƒ์„ ์‰ฝ๊ฒŒ ์•Œ ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์ถœ๋ฐœ์ง€๋Š” s ํ•˜๋‚˜๋กœ ๋‹จ์ผ ์ถœ๋ฐœ์ง€์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ฐ„์„ ์ธ ๋„๋กœ๊ฐ€ ์–‘๋ฐฉํ–ฅ์ด๋ผ๊ณ  ํ•˜๋Š”๊ตฐ์š”. ๋‹ค์ต์ŠคํŠธ๋ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ํ”Œ๋กœ์ด๋“œ ์™€์ƒฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ•ด ๋ณผ ์ˆ˜ ์žˆ๋Š”๋ฐ, ํ•ด๋‹น ๋ฌธ์ œ์—์„œ๋Š” ์ง€์ ์˜ ์ตœ๋Œ€ ๊ฐœ์ˆ˜๊ฐ€ 200 ์ด๋ฏ€๋กœ ์‹œ๊ฐ„ ๋ณต์žก๋„๊ฐ€ O(V3)์ธ ํ”Œ๋กœ์ด๋“œ ์™€์ƒฌ๋กœ ์ ‘๊ทผํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค. 

๊ทธ๋Ÿผ ๊ฐ„์„ ์˜ ์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•ด์ฃผ์–ด ์ธ์ ‘ ๋งคํŠธ๋ฆญ์Šค๋ฅผ ์ดˆ๊ธฐํ™”ํ•ด์ฃผ์–ด์•ผ ํ•˜๋Š”๋ฐ, ์ € ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” ๋ชจ๋“  ์ง€์ ๊ณผ ๊ฐ„์„ ์ด ์ผ์ง์„ ์ƒ์— ์œ„์น˜ํ•  ๋•Œ, ์ฒซ ๋ฒˆ์งธ ์ง€์ ๊ณผ ๋ ์ง€์  ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๊ฐ€ ์ตœ๋Œ€๊ฐ€ ๋œ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ง€์ ์˜ ์ตœ๋Œ€ ๊ฐœ์ˆ˜๊ฐ€ 200์ด๊ณ  ๊ฐ„์„ ์˜ ์ตœ๋Œ€ ๊ธธ์ด๊ฐ€ 10๋งŒ์ด๋ฏ€๋กœ ์ตœ๋Œ€ ๊ธธ์ด(์ตœ๋Œ€ ์š”๊ธˆ)๋Š” 2์ฒœ๋งŒ ๋ฏธ๋งŒ(200 * 100000)์ด๊ฒ ์ฃ ? 

(N + 1) X (N + 1) ํฌ๊ธฐ์˜ 2์ฐจ์› ํ–‰๋ ฌ์„ ์ตœ๋Œ“๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™”์‹œ์ผœ์ฃผ๊ณ  fare๋ฐฐ์—ด์„ ํƒ์ƒ‰ํ•˜๋ฉฐ ๊ฒฝ๋กœ๋งˆ๋‹ค ๊ทธ ๊ฐ’์„ ๊ฐฑ์‹ ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ ํ›„ ํ”Œ๋กœ์ด๋“œ ์™€์ƒฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ๊ฐ ์ง€์  ๊ฐ„ ์ตœ์†Œ ๊ธธ์ด(์š”๊ธˆ)๋ฅผ ๊ตฌํ•ด์ฃผ๊ณ  1๋ถ€ํ„ฐ n ์ง€์  ์‚ฌ์ด๋ฅผ ์ค‘๊ฐ„ ์ง€์ ์œผ๋กœ ์ง€์ •ํ•œ ํ›„ ํ•˜๋‚˜ํ•˜๋‚˜์”ฉ ๊ฐ’์„ ๋น„๊ตํ•˜์—ฌ ์ตœ์†Œ ์š”๊ธˆ์„ ๊ตฌํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค.

 

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


import Foundation

func solution(_ n:Int, _ s:Int, _ a:Int, _ b:Int, _ fares:[[Int]]) -> Int {
    // ์ตœ๋Œ€ ์š”๊ธˆ, ์ตœ๋Œ€์š”๊ธˆ์œผ๋กœ ์ธ์ ‘ ๋งคํŠธ๋ฆญ์Šค ์ดˆ๊ธฐํ™”
    let maxFare = 200 * 100000
    var graph = [[Int]](repeating: [Int](repeating: maxFare, count: n + 1), count: n + 1)
    var answer = Int.max

    for fare in fares {
        let from = fare[0]
        let to = fare[1]
        let cost = fare[2]
        
        // ์–‘๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„
        graph[from][to] = cost
        graph[to][from] = cost
    }
    // ์ž๊ธฐ ์ž์‹ ๊นŒ์ง€์˜ ์š”๊ธˆ๋Š” 0
    for node in 1...n {
        graph[node][node] = 0
    }

    // ํ”Œ๋กœ์ด๋“œ ์™€์ƒฌ: k ์ง€์ ์„ ๊ฑฐ์ณ๊ฐ€๋Š” ๊ฒฝ์šฐ๊ฐ€ ์š”๊ธˆ์ด ์‹ธ๋‹ค๋ฉด, ๊ฐฑ์‹ 
    for k in 1...n {
        for i in 1...n {
            for j in 1...n {
                graph[i][j] = min(graph[i][j], graph[i][k] + graph[k][j])
            }
        }
    }
    // ์ค‘๊ฐ„์ง€์ ์„ ํ•˜๋‚˜์”ฉ ์„ค์ •ํ•˜์—ฌ ์ตœ์†Ÿ๊ฐ’์„ ๊ตฌํ•จ. ์ถœ๋ฐœ์ง€์—์„œ ๊ฐ์ž ์ถœ๋ฐœํ•˜๋Š” ๊ฒฝ์šฐ๋„ ํฌํ•จ (mid == s)
    for mid in 1...n {
        answer = min(answer, graph[s][mid] + graph[mid][a] + graph[mid][b])
    }

    return answer
}

https://programmers.co.kr/learn/courses/30/lessons/72413

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ํ•ฉ์Šน ํƒ์‹œ ์š”๊ธˆ

6 4 6 2 [[4, 1, 10], [3, 5, 24], [5, 6, 2], [3, 1, 41], [5, 1, 24], [4, 6, 50], [2, 4, 66], [2, 3, 22], [1, 6, 25]] 82 7 3 4 1 [[5, 7, 9], [4, 6, 4], [3, 6, 1], [3, 2, 3], [2, 1, 6]] 14 6 4 5 6 [[2,6,6], [6,3,7], [4,6,7], [6,5,11], [2,5,12], [5,3,20], [2,4

programmers.co.kr