Stone Division, Revisited

  • + 0 comments

    Answer in JavaScript:

    function stoneDivision(n, s) {
    const m = {};
    
    function stoneD(pile) {
        if(pile in m) return m[pile];
        let max = 0;
        for(let i of s) {
            if(pile % i === 0 && i !== pile) {
                max = Math.max(max, 1 + (pile / i) * stoneD(i));
            }
        }
        m[pile] = max;
        return max;     
    }
    return stoneD(n);
    }