• + 0 comments

    js

    const map = { 0: 0, 1: 1 };
    function downToZero(n) {
        for (let i = 2; i <= n; i++) {
            if (map[i]) continue;
    
            let a = 2;
            let min = map[i - 1];
            let b;
            while (a <= (b = i / a)) {
                if (Math.floor(b) === b) {
                    min = Math.min(min, map[b]);
                }
                a++;
            }
            map[i] = min + 1;
        }
        return map[n];
    }