C# recursive


  • 0
    H
    public int CountArrangement(int N) {
            bool[] visited = new bool[N + 1];
            return Recurse(1, visited, N);
        }
        
        public int Recurse(int p, bool[] visited, int N) {
            if (p > N) return 1;
            
            int sum = 0;
            for(int i = 1; i <= N; i++) {
                if (visited[i] || (i % p != 0 && p % i != 0)) continue;
                visited[i] = true;
                sum += Recurse(p + 1, visited, N);
                visited[i] = false;
            }
            
            return sum;
        }
    

Log in to reply
 

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