Practice
words = ["wrt", "wrf", "er", "ett", "rftt"], what is the final output string returned by the function?Solution
Step 1: Build graph edges from adjacent words
Edges: w->e (from "wrt" and "wrf"), e->r ("wrf" and "er"), r->t ("er" and "ett"), t->f ("ett" and "rftt")Step 2: Perform topological sort using Kahn's algorithm
Start with nodes with zero in-degree: w; order: w -> e -> r -> t -> fFinal Answer:
Option C -> Option CQuick Check:
Output "wertf" matches topological order from graph [OK]
- Off-by-one in queue processing
- Ignoring some edges
- Returning partial order
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
nums and multiple queries, each query containing two integers x and m. For each query, you want to find the maximum XOR of x with any element in nums that is less than or equal to m. Which algorithmic approach guarantees an optimal solution for large inputs?Solution
Step 1: Understand problem constraints
Brute force is too slow for large inputs; greedy fails because max element ≤ m does not guarantee max XOR.Step 2: Identify optimal approach
Sorting and offline queries allow incremental trie insertion of eligible elements, enabling efficient max XOR queries.Final Answer:
Option D -> Option DQuick Check:
Sorting + trie + offline queries is known optimal [OK]
- Assuming greedy max element ≤ m yields max XOR
- Trying DP over XOR values
- Using brute force for large inputs
[[5,4],[6,4],[6,7],[2,3]], what is the value of the tails list after processing all heights?Solution
Step 1: Sort envelopes and extract heights
Sorted envelopes: [[2,3],[5,4],[6,7],[6,4]] -> heights = [3,4,7,4]Step 2: Trace tails updates
Insert 3 -> tails=[3]; Insert 4 -> tails=[3,4]; Insert 7 -> tails=[3,4,7]; Insert 4 replaces tails[1] -> tails=[3,4,7]Final Answer:
Option A -> Option AQuick Check:
Final tails length is 3 with values [3,4,7] [OK]
- Confusing bisect_left with bisect_right
- Not replacing tails element on equal height
- Misordering envelopes before LIS
Solution
Step 1: Analyze heap operations
Insertion involves pushing into one heap (O(log n)) and possibly popping from the other heap (O(log n)) to rebalance.Step 2: Consider lazy removal pruning
Pruning removes delayed elements lazily, each removal is O(log n), but amortized over many operations remains O(log n) per insertion.Final Answer:
Option B -> Option BQuick Check:
Heap push/pop and pruning dominate, each O(log n) [OK]
- Assuming O(1) insertion ignoring heap operations
- Confusing pruning cost as O(n)
- Thinking sorting is needed each insertion
