Backtracking
1 | class Solution { |
Remarks:
- Prunning is important:
if (i > 0 && nums[i] == nums[i - 1] && !used[i - 1]) continue;
We skip the same items in one layer.!used[i - 1]
means that we can use the duplicated number in different layers (because it was used when we callbacktraking
to process the next layer.)used[i - 1]
only changes tofalse
when a combination is fulfilled andbacktracking
returns. - TC: $O(n!)$, SC: $O(n!\times n)$