Js backtracking solution using closure and IIFE


  • 0
    C
    var countArrangement = function(N) {
        var c = 0;
        var s = [];
        (function helper(index) {
            if (index === N + 1) return c++;
            for (var i = 0; i < N; i++) {
                if (!s[i] && (index % (i + 1) === 0 || (i + 1) % index === 0)) {
                    s[i] = true;
                    helper(index + 1);
                    s[i] = false;
                }
            }
        })(1);
        return c;
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.