Recursive permutation with Java


  • 0
    N
    public static int countArrangement(int N) {
    	       
    		int count  = doThis(N,1,0, new boolean[15]);
    		return count;
    	}
    	
    	private static int doThis(int N, int level, int count, boolean [] helper){
    		
    		if(level > N){
    			count++;
    			//System.out.println("incremented count ");
    			return count;
    		}
    		
    		for (int i = 1; i <= N; i++) {
    			
    			int divident = i;
    			//col index
    			int divisor = level;
    			if(divident % divisor == 0 || divisor % divident == 0){
    				
    				
    				if(!helper[i-1]){
    					//System.out.println("Selected " + i + " at level " + level);
    					helper[i-1] = true; 
    					count = doThis(N, level+1,count,helper);
    					helper[i-1] = false;
    					
    				}
    			}
    		}
    		return count;	
    	}
    

Log in to reply
 

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