Focus On Develop 🀟🀟

[ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€] κΈ°λŠ₯개발 λ³Έλ¬Έ

iOS [Swift]/μ•Œκ³ λ¦¬μ¦˜

[ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€] κΈ°λŠ₯개발

λˆ„λ¦¬λ‹¬μ΄ν‹€ 2020. 12. 27. 22:21

<문제>


<μ „λž΅μ§œκΈ°>

λ¨Όμ € 각 μž‘μ—…μ΄ λͺ‡μΌμ„ ν•„μš”λ‘œ ν•˜λŠ”μ§€ μ•Œμ•„μ•Ό ν•œλ‹€. (100-progress) / speed λ₯Ό ν•˜λ©΄ λ˜λŠ”λ°, 이게 λ”± λ‚˜λˆ„μ–΄ 떨어지지 μ•ŠλŠ” 경우 ν•˜λ£¨κ°€ 더 ν•„μš”ν•˜λ‹€. μ΄λ ‡κ²Œ μž‘μ—…μ‹œκ°„μ΄ κ³„μ‚°λœ Queueλ₯Ό 가지고 메인 λ‘œμ§μ„ μ‹œμž‘ν•œλ‹€. 제일 λ¨Όμ € λ°°ν¬λ˜μ–΄μ•Ό ν•˜λŠ” μž‘μ—…μ˜ μž‘μ—…μ΄ μ™„λ£Œκ°€ λ λ•ŒκΉŒμ§€ μ‹œκ°„μ„ 흘러 보낸닀.(ν•„μš”λ‘œ ν–ˆλ˜ μž‘μ—…μΌμˆ˜μ—μ„œ 1μ”© λΉΌκΈ°) κ·ΈλŸ¬λ‹€ μš°μ„ μˆœμœ„κ°€ κ°€μž₯ 높은 μž‘μ—…μ΄ 배포가 κ°€λŠ₯ν•˜κ²Œ 되면 κ·Έ λ’€λ‘œ 배포가 κ°€λŠ₯ν•œ μž‘μ—…(ν•„μš”λ‘œ ν•˜λŠ” μž‘μ—…μΌμˆ˜ <= 0 )을 λͺ¨λ‘ λΉΌλ©΄μ„œ 배포본수λ₯Ό 1μ”© 더해주고, 배포 μ€€λΉ„κ°€ μ•ˆλœ μž‘μ—…μ΄ μ΅œμš°μ„ μˆœμœ„ μž‘μ—…μ΄ 되면 κ³„μ‚°ν–ˆλ˜ 배포본수λ₯Ό κ²°κ³Ό 배열에 λ„£κ³ , λ˜λ‹€μ‹œ μž‘μ—…μΌμˆ˜λ₯Ό ν˜λŸ¬λ³΄λ‚΄λŠ” (-1ν•΄μ£ΌλŠ”) μž‘μ—…μ„ λ°˜λ³΅ν•œλ‹€.

 

 

<μ½”λ“œ>

import Foundation

func solution(_ progresses:[Int], _ speeds:[Int]) -> [Int] {
    var workDay: [Int] = []
    var res: [Int] = []
    var cnt = 0
    
    for i in 0...progresses.count-1 {
        if  (100 - progresses[i]) % speeds [i] == 0 {
            workDay.append( (100 - progresses[i]) / speeds[i] )
        } else {
            workDay.append( (100 - progresses[i]) / speeds[i] + 1 )
        }
    }
    while !workDay.isEmpty {
        workDay = workDay.map { $0 - 1 }
        while workDay.first! <= 0 {
            workDay.removeFirst()
            cnt += 1
            if workDay.isEmpty {
                break
            }
        }
        if cnt != 0 {
            res.append(cnt)
        }
        cnt = 0
    }
    return res
}

 

<Lesson Learned>

λ¬Έμ œμ—μ„œ μ‹œν‚€λŠ” λŒ€λ‘œ κ΅¬ν˜„ν•˜λ©΄(?) λ˜λŠ” 문제 κ°™λ‹€. 사싀 μ•„λž˜ while λ‘œμ§μ€ μ’€ κ°„λ‹¨νžˆ ν•  수 μžˆλŠ” 방법이 μžˆμ„ 것 같기도 ν•˜μ§€λ§Œ.. 잘λͺ¨λ₯΄κ² λ‹€ πŸ˜‚ πŸ˜‚! 

 

 

 

 

μΆœμ € : ν”„λ‘œκ·Έλž˜λ¨ΈμŠ€ κΈ°λŠ₯개발

Comments