Distinct permutations
Return every distinct permutation of an integer list as lists in lexicographic order. Empty input returns [[]].
from collections import Counter
counts = Counter([1, 1, 2])
counts[1] -= 1
print(counts)
counts[1] += 1Reason about the state
Use a frequency table so equivalent copies of a value do not create duplicate branches. Restore the count after exploring each choice. Before writing code, identify what each variable means after one iteration. Walk through the example and explain why the next transition preserves that meaning. Then check the smallest permitted input and a case with repeated values. A correct answer follows the contract for every input, including the boundary cases in the checks. Keep any input mutation within the stated rules; return a fresh result when the task asks for one.
Your task
Return every distinct permutation of an integer list as lists in lexicographic order. Empty input returns [[]].
The explanation opens here once every check passes.