์‹คํŒจ์œจ - ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค(Swift)

2022. 9. 30. 11:37ใ†ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค-Swift

์‹คํŒจ์œจ - ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค(Swift)

 

 

 ๋ฌธ์ œ ์„ค๋ช…

 

https://school.programmers.co.kr/learn/courses/30/lessons/42889

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

 

 

 

 ๋‚˜์˜ ํ’€์ด

 

import Foundation

func solution(_ N:Int, _ stages:[Int]) -> [Int] {
    
    var totalUser = [Int](repeating: 0, count: N+2)
    var stopUser = [Int](repeating: 0, count: N+2)
    var answerArr = [(Int, Float)](repeating: (0, 0.0), count: N+1) 
    stages.forEach { (stage) in
        for i in (1...stage) {
            totalUser[i] += 1
        } 
        stopUser[stage] += 1
    }
    
    for i in (1...N) {
        answerArr[i] = (i, Float(stopUser[i]) / Float(totalUser[i]))
    }
    
    print(answerArr)
    
    return answerArr.sorted { $0.1 > $1.1 }.map {$0.0}.filter{$0 != 0}
    
}

 

๊ณ ์ฐจํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด์„œ ํ’€๋ ค๊ณ  ํ•˜๋‹ค๊ฐ€ ์‹œ๊ฐ„์ด ์ข€ ๊ฑธ๋ ธ๋‹ค.. ๐Ÿ’ง

์‹คํŒจ์œจ์ฒ˜๋Ÿผ ๋น„์œจ์„ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฒƒ๋“ค์€ ์‹ค์ˆ˜ํ˜•์œผ๋กœ ํ•ด์•ผํ•œ๋‹ค๋Š” ๊ฒƒ๋„ ํ•ญ์ƒ ์—ผ๋‘ํ•ด๋‘ฌ์•ผ๊ฒ ๋‹ค.

 

 

 

 

 ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด

 

๊ต‰์žฅํžˆ ์ง๊ด€์ ์ด๊ณ  ๋…ผ๋ฆฌ์ ์ด์—ฌ์„œ ๊ฐ€์ ธ์™”๋‹ค.

import Foundation

func solution(_ N:Int, _ stages:[Int]) -> [Int] {
 
    var failure: [Int: Float] = [:]
    var player: Int = stages.count
    
    for i in 1...N {
        let n = stages.filter { $0 == i }.count
        failure[i] = Float(n)/Float(player)
        player -= n
    }

    return failure.sorted(by: <).sorted(by: { $0.value > $1.value }).map {$0.key}
}

 

1๋ถ€ํ„ฐ N๊นŒ์ง€ ๋Œ๋ฉด์„œ ํƒ์ƒ‰ํ•˜๋Š” i๊ฐ€ stages์—์„œ ๋ฉˆ์ถฐ์žˆ๋Š” ์Šคํ…Œ์ด์ง€ ์ˆซ์ž์™€ ๊ฐ™์„ ๋•Œ๋ฅผ n์œผ๋กœ ์ง€์ •ํ•˜๋ฉด ์ด n์€ ํŠน์ • ์ˆซ์ž๊นŒ์ง€ ๋„๋‹ฌํ–ˆ์ง€๋งŒ ๊นจ์ง€๋ชปํ•œ ๊ฒฝ์šฐ๊ฐ€ ๋œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  player์—์„œ๋Š” ์ „์ฒด ์ˆซ์ž์—์„œ ํƒ์ƒ‰ํ• ๋•Œ๋งˆ๋‹ค n์„ ๋นผ์คŒ์œผ๋กœ์จ ๊ทธ ๋‹ค์Œ ํƒ์ƒ‰ํ•˜๋Š” ๊ฐ’์€ ์ด์ „์— ๊ฐ’๋“ค์ด ๋„๋‹ฌํ•  ์ˆ˜ ์—†๋Š” ๊ฒƒ์„ ํ‘œํ˜„ํ–ˆ๋‹ค.

์˜ˆ์‹œ๋กœ [2, 3, 4, 5]๋ผ๋Š” stages๋ฐฐ์—ด์ด ์žˆ์„๋•Œ i๊ฐ€ 1๋ถ€ํ„ฐ 5๊นŒ์ง€ ๋Œ๋ฉด์„œ 2๊ฐ€ ๋˜์—ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ณด์ž. ์ด๋•Œ 2์ธ ๊ฐ’์ด ํ•˜๋‚˜๊ฐ€ ์žˆ์–ด์„œ player.count์ธ 4์—์„œ 1์„ ๋นผ์ค€๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‹ค์Œ i๊ฐ€ 3์ด๋ ๋•Œ๋Š” player.count๊ฐ€ 3์ธ ์ƒํƒœ๋กœ ์‹œ์ž‘๋œ๋‹ค. ๊ทธ ์ด์œ ๊ฐ€ 2๋Š” 3์— ๋„๋‹ฌํ•˜์ง€ ๋ชปํ•œ ์ˆซ์ž๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. 

 

ํ•˜์ง€๋งŒ ์ด ์ฝ”๋“œ๋Š” ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋‚œ๋‹ค. 

stages์˜ ๊ธธ์ด๊ฐ€ 200,000์ด๊ณ  n์€ 500๊นŒ์ง€๊ธฐ ๋–„๋ฌธ์— ์ด ๋‘˜์„ ๊ณฑํ•ด๋ณด๋ฉด 1์–ต์ด ๋œ๋‹ค. 

๊ทธ๋ž˜์„œ ์ง๊ด€์ ์ด๊ณ  ์ข‹์€ ์ฝ”๋“œ์ง€๋งŒ ์‹œ๊ฐ„๋ณต์žก๋„ ์ธก๋ฉด์—์„œ๋Š” ์•„์‰ฌ์šด ์ฝ”๋“œ์ธ ๊ฒƒ ๊ฐ™๋‹ค. (์ฝ”๋”ฉํ…Œ์ŠคํŠธ์—์„œ๋Š”)

 

 

 ํ”ผ๋“œ๋ฐฑ

 

์ด ๋ฐ–์—๋„ ์—ฌ๋Ÿฌ ์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ณด์•˜๋Š”๋ฐ ์ง๊ด€์ ์ด์ง€๋งŒ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚˜์˜ค๋Š” ์ฝ”๋“œ๊ฐ€ ๊ต‰์žฅํžˆ ๋งŽ์•˜๋‹ค.

๋งค ์ˆœ๊ฐ„ ์ฝ”๋“œ์˜ ๊ฐ„๊ฒฐํ•จ๊ณผ ์„ฑ๋Šฅ ์‚ฌ์ด์—์„œ ์ ์ ˆํ•จ์„ ์ฐพ๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•œ ๊ฒƒ ๊ฐ™๋‹ค.