// solution :: Array<number> -> Array<number> -> Array<number>
// TODO: 시간복잡도 개선
// TODO: 명령형으로 작성된 코드를 함수형으로 개선 √
// TODO: 스택 활용하여 구현
// TODO: 다른 사람들 풀이 보고 숙지
// - 콜스택을 활용한 함수형(?) 구현
// - 속도는 이 구현이 오히려 더 느리다.
function solution(progresses, speeds) {
// getSpeedsFromProgresses :: Array<number> -> Array<number> -> Array<number>
const getSpeedsFromProgresses = (progresses, speeds) => {
return speeds.slice(-progresses.length);
}
// sumCount :: Array<number> -> number -> [number, Array<number>]
const sumCompletedFeature = (progresses, count = 0) => {
if (progresses.length === 0) return [count, progresses];
if (progresses[0] >= 100) return sumCompletedFeature(progresses.slice(1), count + 1);
else return [count, progresses];
}
// recur :: Array<number> -> Array<number> -> Array<number>
return (function recur(progresses, speeds, result = []) {
if (progresses.length === 0) return result;
const progressesInADay = progresses.map((progress, idx) => progress + speeds[idx]);
const [currentSum, currentProgresses] = sumCompletedFeature(progressesInADay);
const currentResult = currentSum === 0 ? result : [...result, currentSum];
return recur(
currentProgresses,
getSpeedsFromProgresses(currentProgresses, speeds),
currentResult
);
})(progresses, speeds);
};
/* - 명령형 구현
function solution(progresses, speeds) {
var answer = [];
while (speeds.length > 0) {
progresses.forEach((progress, idx) => {
progresses[idx] = progress + speeds[idx];
});
let count = 0;
while (progresses[0] >= 100) {
progresses.shift();
speeds.shift();
count += 1;
}
if (count > 0) answer.push(count);
}
return answer;
}
*/