Practice
def maxProfit(k, prices):
n = len(prices)
if k >= n // 2:
profit = 0
for i in range(1, n):
profit += max(0, prices[i] - prices[i - 1])
return profit
next_dp = [[0, 0] for _ in range(k + 1)]
curr_dp = [[0, 0] for _ in range(k + 1)]
for i in range(n - 1, -1, -1):
for t in range(1, k + 1):
curr_dp[t][0] = max(-prices[i] + next_dp[t][1], next_dp[t][0])
curr_dp[t][1] = max(prices[i] + next_dp[t - 1][0], next_dp[t][1])
next_dp, curr_dp = curr_dp, next_dp
return next_dp[k][0]
print(maxProfit(1, [1, 5, 2]))Solution
Step 1: Trace dp for i=2 (price=2), t=1
Initialize next_dp and curr_dp with zeros. At i=2, curr_dp[1][0] = max(-2 + next_dp[1][1], next_dp[1][0]) = max(-2 + 0, 0) = 0; curr_dp[1][1] = max(2 + next_dp[0][0], next_dp[1][1]) = max(2 + 0, 0) = 2.Step 2: Trace dp for i=1 (price=5), t=1
curr_dp[1][0] = max(-5 + next_dp[1][1], next_dp[1][0]) = max(-5 + 2, 0) = 0; curr_dp[1][1] = max(5 + next_dp[0][0], next_dp[1][1]) = max(5 + 0, 2) = 5.Step 3: Trace dp for i=0 (price=1), t=1
curr_dp[1][0] = max(-1 + next_dp[1][1], next_dp[1][0]) = max(-1 + 5, 0) = 4; curr_dp[1][1] = max(1 + next_dp[0][0], next_dp[1][1]) = max(1 + 0, 5) = 5.Final Answer:
Option A -> Option AQuick Check:
Maximum profit with 1 transaction is buying at 1 and selling at 5 -> profit 4 [OK]
- Confusing holding and not holding states
- Off-by-one errors in indexing transactions
- Misreading the final dp state to return
Solution
Step 1: Trace helper on left child (2):
left and right children are null, so left=0, right=0, max_sum updated to max(-inf, 2+0+0)=2, returns 2+max(0,0)=2.Step 2: Trace helper on right child (3):
Similarly, max_sum updated to max(2, 3+0+0)=3, returns 3+max(0,0)=3.Step 3: Trace helper on root (1):
left=2, right=3, max_sum updated to max(3, 1+2+3)=6, returns 1+max(2,3)=4.Final Answer:
Option B -> Option BQuick Check:
Max path is 2 -> 1 -> 3 with sum 6 [OK]
- Returning sum of one child only
- Off-by-one in max_sum update
- Ignoring zero cutoff for negative gains
Solution
Step 1: Understand the problem constraints
The problem requires finding the median of two sorted arrays without fully merging them to achieve better than O(m+n) time.Step 2: Identify the optimal approach
Binary search on the smaller array to find a partition that balances elements on both sides guarantees O(log(min(m,n))) time, which is optimal.Final Answer:
Option B -> Option BQuick Check:
Binary search on partition is the known optimal solution [OK]
- Assuming full merge is necessary
- Using two pointers without binary search
- Confusing DP with median finding
k sorted lists of integers. Your task is to find the smallest range that includes at least one number from each of the k lists. Which algorithmic approach guarantees an optimal solution for this problem?Solution
Step 1: Understand problem constraints
The problem requires a range covering at least one element from each list, which suggests tracking coverage across lists.Step 2: Evaluate approaches
Greedy alone fails because it doesn't guarantee minimal range; DP is not suitable due to combinatorial explosion; brute force enumerates all combos but is inefficient. Merging all lists and using two pointers efficiently tracks coverage and minimal range.Final Answer:
Option B -> Option BQuick Check:
Two-pointer on merged array is known optimal [OK]
- Thinking greedy alone suffices
- Assuming DP applies directly
- Using brute force for large inputs
Solution
Step 1: Identify Trie construction cost
Building the Trie takes O(W * L) time, inserting each word character by character.Step 2: Analyze backtracking complexity
Backtracking explores up to 4^L paths from each cell, so O(N * M * 4^L) dominates search time.Final Answer:
Option B -> Option BQuick Check:
Trie build plus backtracking combined yields O(W*L + N*M*4^L) [OK]
- Confusing independent word search with Trie pruning
- Ignoring Trie build cost
- Multiplying all parameters incorrectly
