Math.ceil((100 - progress) / speed)
현재 기능의 배포 가능한 날짜가 앞선 기능의 배포 가능 날짜보다 작거나 같을 경우, 이전 작업과 함께 배포될 수 있다.
한 번의 순회로 확인하려면, stack을 이용하여 stack top에 있는 기능의 배포 가능 날짜와 비교해야 한다.
배포 가능한 날짜를 담은 days stack과 각 날짜에 배포할 기능의 수를 담은 count stack을 선언한다.
기능을 순회하며 다음을 반복한다.
Math.ceil((100 - progress) / speed)
count stack을 반환한다.
class Stack {
constructor() {
this.s = [];
}
empty() {
return this.s.length === 0;
}
top() {
return this.s[this.s.length - 1];
}
push(item) {
this.s.push(item);
}
pop() {
const [top] = this.s.splice(this.s.length - 1, 1);
return top;
}
values() {
return [...this.s];
}
}
function solution(progresses, speeds) {
const count = new Stack();
const days = new Stack();
progresses.forEach((progress, i) => {
const day = Math.ceil((100 - progress) / speeds[i]);
if (days.empty() || days.top() < day) {
days.push(day);
count.push(1);
} else {
const top = count.pop();
count.push(top + 1);
}
});
return count.values();
}