Share My Backtrack solution


  • 3
    S
    int res;
    public int countArrangement(int N) {
    	res = 0;
    	List<Integer> list = new ArrayList<>();
    	for (int i = 1; i <= N; i++) list.add(i);
    	helper(list, 1, N);
    	return res;
    }
    		
    private void helper(List<Integer> list, int id, int n) {
    	if (id > n) {
    		res++;
    		return;
    	}
    	for (int i = 0; i < list.size(); i++) {
    		if (list.get(i) %id == 0 || id % list.get(i) == 0){
    			int temp = list.remove(i);
    			helper(list, id+1, n);
    			list.add(i, temp);
    		}
    	}
    }
    

Log in to reply
 

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