Practice
def findCheapestPrice(n, flights, src, dst, K):
INF = float('inf')
prev = [INF] * n
prev[src] = 0
for _ in range(K + 1):
curr = prev[:]
for u, v, w in flights:
if prev[u] != INF:
curr[v] = min(curr[v], prev[u] + w)
prev = curr
return prev[dst] if prev[dst] != INF else -1
print(findCheapestPrice(n, flights, src, dst, K))Solution
Step 1: Trace dp arrays for each iteration
Initially prev = [0, inf, inf]. After first iteration (0 stops), curr updates cost to city 1 as 100 and city 2 as 500. After second iteration (1 stop), curr updates city 2 cost to min(500, 100 + 100) = 200.Step 2: Return final cost for destination
prev[2] after K+1=2 iterations is 200, which is the cheapest cost with at most 1 stop.Final Answer:
Option B -> Option BQuick Check:
DP updates costs correctly over K+1 iterations [OK]
- Returning direct edge cost ignoring stops
- Off-by-one in iteration count
- Confusing curr and prev arrays
Solution
Step 1: Identify loops in the algorithm
The outer loop runs K+1 times, and the inner loop iterates over all E flights to relax edges.Step 2: Calculate total complexity
Each iteration processes E edges, so total time is O(K * E). No nested loops over n^2 or log factors appear.Final Answer:
Option A -> Option AQuick Check:
Algorithm iterates over edges K+1 times [OK]
- Confusing E with n^2
- Assuming Dijkstra complexity
- Counting recursion stack space
Solution
Step 1: Examine dp update line
The original code has dp[j] = triangle[i][j] + min(dp[j], dp[j + 1]). The buggy code swaps the min arguments to min(dp[j + 1], dp[j]) which is harmless since min is commutative.Step 2: Check for index out of bounds risk
Accessing dp[j + 1] is safe because dp length is one more than current row length, so no out-of-bounds here.Step 3: Identify subtle bug
The subtle bug is that dp is updated in place from left to right, which can cause dp[j + 1] to be already updated when used in min, leading to incorrect results.Step 4: Explanation of bug
Because dp is updated from left to right, dp[j + 1] may have been updated in the current iteration, so min(dp[j], dp[j + 1]) uses a mixed state of dp values.Step 5: Correct fix
To fix, update dp from right to left so dp[j + 1] is not yet updated when used.Final Answer:
Option B -> Option BQuick Check:
Updating dp in wrong order causes subtle bugs [OK]
- Swapping min arguments (harmless but suspicious)
- Index out of bounds on dp[j+1]
- Updating dp in wrong order causing overwritten values
m x n grid using a space-optimized DP approach:def uniquePaths(m, n):
dp = [1] * n
for i in range(1, m):
for j in range(1, n):
dp[j] = dp[j] + dp[j]
return dp[-1]What is the bug in this code?Solution
Step 1: Analyze inner loop update
Line 4 updates dp[j] by adding dp[j] to itself, doubling the value instead of adding dp[j-1].Step 2: Identify correct update
The correct update is dp[j] += dp[j - 1] to accumulate paths from left and top cells.Final Answer:
Option B -> Option BQuick Check:
Doubling dp[j] breaks path count logic -> bug at line 4 [OK]
- Using dp[j] + dp[j] instead of dp[j] + dp[j-1]
- Off-by-one errors in loops
Solution
Step 1: Identify role of remove_consecutive
After insertion and removal, consecutive balls must be recursively removed to handle chain reactions.Step 2: Locate missing recursive removal
The line creating new_s omits calling remove_consecutive, so chain reactions are not processed, leading to incorrect board states and minimal moves.Final Answer:
Option A -> Option AQuick Check:
Missing recursive removal causes wrong minimal moves [OK]
- Forgetting to call remove_consecutive after insertion
- Incorrectly adjusting hand counts
- Miscomputing balls needed for removal
