Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Algorithm] 기능개발 #170

Closed
hwangJi-dev opened this issue Mar 31, 2023 · 0 comments
Closed

[Algorithm] 기능개발 #170

hwangJi-dev opened this issue Mar 31, 2023 · 0 comments

Comments

@hwangJi-dev
Copy link
Owner

hwangJi-dev commented Mar 31, 2023

💬 문제

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


💬 Idea

  • 기능별 남은 진척도를 remains 배열에 저장하고, 앞 기능이 개발완료되었을 때 꺼내어지는(같이 기능개발이 완료되어 배포되는) 기능을 스택 구조로 체크한다.

💬 풀이

import Foundation

func solution(progresses:[Int], speeds:[Int]) -> [Int] {
    var remains: [Int] = []
    
    for (idx, progress) in progresses.enumerated() {
        let remain = ((100 - progress) / speeds[idx]) + (((100 - progress) % speeds[idx]) == 0 ? 0 : 1)
        remains.append(remain)
    }
    
    remains = remains.reversed()
    
    var distributionCount = 0
    var distributionDay = 0
    var res: [Int] = []
    
    if let rem = remains.popLast() {
        distributionDay = rem
        distributionCount = 1
    }
    
    while !remains.isEmpty {
        if distributionDay >= remains.last! {
            remains.popLast()
            distributionCount += 1
        } else {
            distributionDay = remains.popLast() ?? 0
            res.append(distributionCount)
            distributionCount = 1
        }
    }
    
    return distributionCount == 0 ? res : res + [distributionCount]
}

소요시간 : 26분


💬 더 나은 방법?

func solution(progresses:[Int], speeds:[Int]) -> [Int] {
    var res: [Int] = []
    var count = 0
    var day = 0
    
    for (idx, progress) in progresses.enumerated() {
        let remain = ((100 - progress) / speeds[idx]) + (((100 - progress) % speeds[idx]) == 0 ? 0 : 1)
        if day == 0 {
            day = remain
        }
        
        if day < remain {
            day = remain
            res.append(count)
            count = 1
        } else {
            count += 1
        }
    }
    
    return count > 0 ? res + [count] : res
}

💬 알게된 문법

✅ ceil

  • 소수점 올림시 사용

✅ floor

  • 소수점 내림시 사용

✅ round

  • 소수점 반올림시 사용
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant