Closed
Description
💬 문제
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
- 소수점 반올림시 사용