You are viewing a single comment's thread. Return to all comments →
My solution in JavaScript. Going from 1 to N in spite of N -> 1 allowed me not to run in maximum call stack size problems.
let savedResults = {}; function minMove(n) { if (n <= 3) return n; if (savedResults[n] > 0) return savedResults[n]; let min = Number.MAX_SAFE_INTEGER; for (var i = 2, len = Math.floor(Math.sqrt(n)); i <= len; i++) { if (n % i === 0) { let factor = Math.floor(n / i); min = Math.min(min, 1 + minMove(factor)); } } min = Math.min(min, 1 + minMove(n - 1)); savedResults[n] = min; return min; } function main() { let Q = parseInt(readLine()); for (let a0 = 0; a0 < Q; a0++) { N = parseInt(readLine()); if (N <= 3) { console.log(N); } else { for (var i = 4; i <= N; i++) { minMove(i); } console.log(minMove(N)); } } }
Seems like cookies are disabled on this browser, please enable them to open this website
Down to Zero II
You are viewing a single comment's thread. Return to all comments →
My solution in JavaScript. Going from 1 to N in spite of N -> 1 allowed me not to run in maximum call stack size problems.