Practice
def minDominoRotations(A, B):
def check(x):
rotations_a = rotations_b = 0
for i in range(len(A)):
if A[i] != x and B[i] != x:
return -1
elif A[i] != x:
rotations_a += 1
elif B[i] != x:
rotations_b += 1
return min(rotations_a, rotations_b)
rotations = check(A[0])
if rotations != -1:
return rotations
else:
return check(B[0])
A = [3,5,1,2]
B = [3,6,3,3]
print(minDominoRotations(A, B))Solution
Step 1: Check candidate x = A[0] = 3
i=0: A[0]=3 matches x=3, no rotation.
i=1: A[1]=5 != 3, B[1]=6 != 3 -> return -1 immediately.Step 2: Check candidate x = B[0] = 3
i=0: A[0]=3 matches x=3, no rotation.
i=1: A[1]=5 != 3, B[1]=6 != 3 -> return -1 immediately.
Both candidates fail, so output is -1.Final Answer:
Option A -> Option AQuick Check:
Both candidates fail at i=1, so no solution [OK]
- Assuming first candidate always works
- Miscounting rotations when both sides match
def canCompleteCircuit(gas, cost):
n = len(gas)
net = [gas[i] - cost[i] for i in range(n)]
# Bug: missing total gas check
prefix = [0] * (2 * n + 1)
for i in range(2 * n):
prefix[i+1] = prefix[i] + net[i % n]
for i in range(n):
if prefix[i+n] - prefix[i] >= 0:
return i
return -1Solution
Step 1: Identify missing total gas check
The code does not check if sum(net) < 0 before proceeding, which can cause incorrect start index or false positives.Step 2: Verify other lines
Net array, prefix sums, and prefix difference checks are correct and standard.Final Answer:
Option B -> Option BQuick Check:
Missing total gas check leads to incorrect results [OK]
- Forgetting total gas check
- Misusing modulo in prefix sums
- Resetting start without resetting tank
def twoCitySchedCost(costs):
costs.sort(key=lambda x: abs(x[0] - x[1]))
n = len(costs) // 2
total = 0
for i, cost in enumerate(costs):
if i < n:
total += cost[0]
else:
total += cost[1]
return total
Solution
Step 1: Analyze sorting key
The code sorts by absolute difference abs(cost[0] - cost[1]) instead of signed difference cost[0] - cost[1].Step 2: Effect of wrong sorting
Sorting by absolute difference loses the direction of cost preference, causing suboptimal assignments and higher total cost.Final Answer:
Option A -> Option AQuick Check:
Signed difference sorting is required for correct greedy assignment [OK]
- Sorting by absolute difference
- Miscounting half the people
- Swapping city costs in assignment
Solution
Step 1: Understand the impact of backward jumps
Backward jumps mean the problem is no longer monotonic; maxReach tracking fails as reachable indices can decrease.Step 2: Identify suitable approach
BFS or graph traversal explores all reachable indices in any direction, correctly handling negative jumps.Step 3: Explain why other options fail
Greedy fails due to backward jumps; DP recursion is possible but less efficient; sorting is irrelevant.Final Answer:
Option B -> Option BQuick Check:
BFS explores all reachable nodes regardless of jump direction [OK]
- Trying to apply greedy despite backward jumps
- Assuming sorting helps reachability
Solution
Step 1: Understand cycles and revisits
Allowing reuse means indices can be revisited, so visited set alone blocks better paths.Step 2: Use distance array for shortest paths
Tracking minimum jumps per index and updating when a shorter path is found prevents infinite loops and finds optimal jumps.Step 3: Why other options fail
Removing visited causes infinite loops; resetting visited loses pruning; keeping visited blocks revisits.Final Answer:
Option D -> Option DQuick Check:
Distance array with updates handles cycles correctly [OK]
- Removing visited set causing infinite loops
- Resetting visited incorrectly
- Assuming original BFS works unchanged
