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
def maximalSquare(matrix):
if not matrix or not matrix[0]:
return 0
rows, cols = len(matrix), len(matrix[0])
dp = [0] * (cols + 1)
max_side = 0
prev = 0
for i in range(rows):
for j in range(1, cols + 1):
temp = dp[j]
if matrix[i][j-1] == '1':
dp[j] = 1 + min(dp[j], dp[j-1], prev)
max_side = max(max_side, dp[j])
else:
dp[j] = 0
prev = temp
return max_side * max_side
Solution
Step 1: Trace dp array updates for each row
Row 0: dp updates to [0,1,0,1], max_side=1; Row 1: dp updates to [0,1,1,1], then dp[3]=1+min(1,1,1)=2, max_side=2; Row 2: dp updates to [0,1,2,0], max_side remains 2.Step 2: Calculate final area
max_side=2, so area = 2*2 = 4.Final Answer:
Option B -> Option BQuick Check:
Max square side 2 -> area 4 [OK]
- Off-by-one in indexing dp array
- Confusing prev and temp updates
- Returning max_side instead of area
Solution
Step 1: Identify loops in bottom-up DP
There are three nested loops: length of interval (up to n), start index i (up to n), and position k (up to n) inside the interval.Step 2: Calculate complexity
Each loop runs up to n times, so total time is O(n * n * n) = O(n^3). The dp table is size O(n^2), but filling it requires the third loop over k.Final Answer:
Option B -> Option BQuick Check:
Three nested loops over n -> O(n^3) time complexity [OK]
- Confusing dp table size with time complexity
- Ignoring the innermost loop over k
- Assuming exponential time for DP solution
def minFallingPathSum(matrix):
n = len(matrix)
dp = matrix[0][:]
for i in range(1, n):
for j in range(n):
best = dp[j]
if j > 0:
best = min(best, dp[j-1])
if j < n - 1:
best = min(best, dp[j+1])
dp[j] = matrix[i][j] + best
return min(dp)
Solution
Step 1: Understand dp update logic
dp array holds minimum sums for previous row. Updating dp[j] in place overwrites values needed for neighbors in the same iteration.Step 2: Identify the bug
Updating dp[j] inside the inner loop without using a separate array causes premature overwriting, leading to incorrect neighbor comparisons.Final Answer:
Option A -> Option AQuick Check:
Using a separate new_dp array fixes the bug [OK]
- Updating dp in place without backup
- Ignoring boundary checks
Solution
Step 1: Analyze time complexity
The algorithm iterates over each cell once in nested loops: O(m*n).Step 2: Analyze space complexity
Uses a single 1D dp array of length n, so space is O(n).Final Answer:
Option C -> Option CQuick Check:
Nested loops over m and n, dp array size n [OK]
- Confusing recursion stack space with dp array
- Assuming full 2D dp array used
- Mistaking linear time for exponential
