Time complexity: O(n × n!). You generate n! permutations, each takes O(n) to copy.
Space complexity: O(n) for recursion depth and the used array.
This is the standard backtracking template for permutations.
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
(Python solution)
Time complexity: O(n × n!). You generate n! permutations, each takes O(n) to copy.
Space complexity: O(n) for recursion depth and the used array.
This is the standard backtracking template for permutations.