Practice
[1,1,2]?
from typing import List
def permuteUnique(nums: List[int]) -> List[List[int]]:
nums.sort()
res = []
def backtrack(start=0):
if start == len(nums):
res.append(nums[:])
return
seen = set()
for i in range(start, len(nums)):
if nums[i] in seen:
continue
seen.add(nums[i])
nums[start], nums[i] = nums[i], nums[start]
backtrack(start + 1)
nums[start], nums[i] = nums[i], nums[start]
backtrack()
return res
print(permuteUnique([1,1,2]))Solution
Step 1: Trace initial sorting and first level of recursion
Input sorted to [1,1,2]. At start=0, i iterates over 0 to 2. First, swap nums[0] with nums[0] (1), add 1 to seen, recurse.Step 2: Trace subsequent recursion and skipping duplicates
At start=1, i iterates 1 to 2. Swap nums[1] with nums[1] (1), add 1 to seen, recurse. Then swap nums[2] with nums[1] (2), add 2 to seen, recurse. Duplicate 1 at i=2 skipped due to seen.Final Answer:
Option C -> Option CQuick Check:
Output matches expected unique permutations in lex order [OK]
- Misordering output permutations due to swap order
Solution
Step 1: Understand the problem requires all permutations
Generating all permutations means enumerating every possible ordering of the input elements without duplicates.Step 2: Identify the approach that systematically explores all orderings
Backtracking with a used array or in-place swapping explores all permutations by recursively building sequences or swapping elements, ensuring no duplicates and covering all possibilities.Final Answer:
Option A -> Option AQuick Check:
Backtracking is the standard method for generating all permutations [OK]
- Thinking greedy or sorting can generate all permutations without duplicates
def get_candidates(r, c):
b = (r//3)*3 + c//3
return [ch for ch in '123456789' if ch not in rows[r] and ch not in cols[c] and ch not in boxes[b]]
empties.sort(key=lambda x: len(get_candidates(x[0], x[1])))
first_empty = empties[0]
candidates_len = len(get_candidates(first_empty[0], first_empty[1]))
What is the value of candidates_len?Solution
Step 1: Identify first empty cell after sorting
Check empties and compute candidates for each; the cell with fewest candidates is chosen first.Step 2: Calculate candidates for first empty cell (0,2)
Row 0 has {'5','3','7'}, column 2 has {'8'}, box 0 has {'5','3','6','9','8'}. Candidates are digits not in any of these sets: '1','2','4'. So length is 3.Final Answer:
Option B -> Option BQuick Check:
Counting candidates for (0,2) yields 3 [OK]
- Forgetting box constraints
- Miscounting candidates for first empty cell
def partition(s):
def is_palindrome(left, right):
while left < right:
if s[left] != s[right]:
return False
left += 1
right -= 1
return True
def backtrack(start, path):
if start == len(s):
result.append(path) # Line X
return
for end in range(start, len(s)):
if is_palindrome(start, end):
path.append(s[start:end+1])
backtrack(end+1, path)
path.pop()
result = []
backtrack(0, [])
return result
Solution
Step 1: Identify how results are stored
Appendingpathdirectly stores a reference, so later modifications affect all stored results.Step 2: Correct approach
Useresult.append(path[:])to append a copy, preserving each partition snapshot.Final Answer:
Option B -> Option BQuick Check:
Shared references cause incorrect final partitions [OK]
- Forgetting to copy path before appending
- Incorrect palindrome check boundaries
- Forgetting to pop after recursion
Solution
Step 1: Identify missing visited marking
The code does not mark the current cell as visited before recursive calls, allowing revisits.Step 2: Understand impact of missing marking
Without marking, dfs can revisit the same cell multiple times, causing incorrect matches or infinite loops.Final Answer:
Option D -> Option DQuick Check:
Marking visited cells is essential to prevent reuse [OK]
- Forgetting to mark visited cells
- Restoring cell too early
