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

2022. 10. 14. 13:43ใ†ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค-Swift

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

 

 

 ๋ฌธ์ œ ์„ค๋ช…

 

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

 

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

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

programmers.co.kr

 

 

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

 

idx๋ฅผ ์ด์šฉํ•ด์„œ ํ’€์€ ํ’€์ด์ด๋‹ค. 

์ฒ˜์Œ ํ’€์ด๋ฅผ ํ–ˆ์„๋•Œ๋Š” idx์™€ Idx+1์˜ ๊ฒฝ์šฐ๋งŒ ์ฒดํฌํ•ด์„œ ๊ฒฐ๊ณผ๊ฐ’์— ๋„ฃ์—ˆ๋Š”๋ฐ, ๊ทธ๋Ÿผ 5 3 4์ธ ๊ฒฝ์šฐ์— 3์€ 4๋ณด๋‹ค ์ž‘๊ธฐ๋•Œ๋ฌธ์— ๊ฒฐ๊ณผ๊ฐ’์— 2, 1์ด ๋“ค์–ด๊ฐ”๋‹ค. (์›๋ž˜๋Š” 3, 4๊ฐ€ 5๋ณด๋‹ค ์ž‘๊ธฐ๋–„๋ฌธ์— 1๋งŒ ๋„ฃ์–ด์•ผ ํ•จ)

๊ทธ๋ž˜์„œ ์ฒ˜์Œ ๊ฐ’์„ ๊ธฐ๋กํ•˜๊ณ  ๊ทธ ๊ฐ’๊ณผ idx+1์„ ๋น„๊ตํ•ด์„œ ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ๋‹ค.  

 

import Foundation

func solution(_ progresses:[Int], _ speeds:[Int]) -> [Int] {
    var days = progresses.indices.map { ceil(Double(100 - progresses[$0]) / Double(speeds[$0])) }
    var result: [Int] = []
    var idx = 0
    var cnt = 1
    var maxVal = 0.0
    while true {
        if idx == days.count-1 {
            break
        } else {
            maxVal = max(maxVal, days[idx])
            if maxVal >= days[idx+1] {
                cnt += 1
            } else {
                result.append(cnt)
                cnt = 1
                maxVal = 0.0
            }
            idx += 1
        }
    }
    result.append(cnt)
    
    return result
}

 

 

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

 

๋‹ค๋ฅธ ์‚ฌ๋žŒ์€ else๋ฌธ์—์„œ lastReleaseDate๋ฅผ ์ƒˆ๋กœ์šด ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™”์‹œํ‚ค์ง€ ์•Š๊ณ  ํ˜„์žฌ ์ธ๋ฑ์Šค๊ฐ€ ์ž‘๋‹ค๋ฉด cnt๋งŒ ์ฆ๊ฐ€์‹œํ‚ค๋Š” ํ˜•์‹์œผ๋กœ ํ’€์ดํ–ˆ๋‹ค. 

ํƒ์ƒ‰ํ•˜๋ฉด์„œ ์•ž์— ๊ฐ’๋ณด๋‹ค ํฐ ๊ฐ’๋งŒ์ด lastReleaseDate๊ฐ€ ๋  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด ํ•ต์‹ฌ์ด๋‹ค. (์ž‘๋‹ค๋ฉด ๊ทธ๊ฑด ํฌํ•จํ•ด์„œ ๋ฐฐํฌ ์‹œํ‚ค๊ธฐ ๋•Œ๋ฌธ)

 

import Foundation

func solution(_ progresses:[Int], _ speeds:[Int]) -> [Int] {
    var lastReleaseDate: Int = 0
    var result: [Int] = []
    
    for i in 0..<progresses.count {
        let progress = Double(progresses[i])
        let speed = Double(speeds[i])
        let day = Int(ceil((100 - progress) / speed))
        if day > lastReleaseDate {
            lastReleaseDate = day
            result.append(1)
        } else {
            result[result.count - 1] += 1
        }
    }
    
    
    return result
}

 

 

 

 ํ”ผ๋“œ๋ฐฑ

 

๋ฌธ์ œ์˜ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์ผ€์ด์Šค๋ฅผ ์Šค์Šค๋กœ ๋งŒ๋“ค๊ณ  ๋‹ค์–‘ํ•˜๊ฒŒ ๋Œ€์ž…ํ•ด์„œ ํ‘ธ๋Š” ๊ฒŒ ํ•„์š”ํ•˜๋‹ค.