Practice
def maxProfit(prices):
i = 0
profit = 0
n = len(prices)
while i < n - 1:
while i < n - 1 and prices[i] >= prices[i + 1]:
i += 1
valley = prices[i]
while i < n - 1 and prices[i] <= prices[i + 1]:
i += 1
peak = prices[i]
profit += peak - valley
return profit
Solution
Step 1: Trace first while loop to find valley
i=0, prices[0]=1, prices[1]=2, 1 < 2 so inner loop skips, valley=1Step 2: Trace second while loop to find peak
i increments while prices[i] <= prices[i+1]: i=0 to 1 (2 <= 3), i=1 to 2 (3 no next), peak=3Step 3: Calculate profit and return
profit += 3 - 1 = 2, loop ends, return 2Final Answer:
Option A -> Option AQuick Check:
Profit matches sum of positive differences (2) [OK]
- Off-by-one error missing last peak
- Confusing valley and peak assignments
- Returning zero if no decreasing sequence found
n units of time before executing the same task again. Which approach guarantees the minimum total time to finish all tasks?Solution
Step 1: Understand the cooldown constraint
The CPU must waitnunits before repeating the same task, so scheduling must consider task frequencies and cooldowns.Step 2: Why max-heap greedy works best
Using a max-heap prioritizes tasks with the highest remaining frequency, ensuring minimal idle time by always picking the most urgent task available.Final Answer:
Option B -> Option BQuick Check:
Max-heap approach matches known optimal solution [OK]
- Assuming DP or brute force is needed
- Ignoring cooldown leads to incorrect minimal time
- Greedy without priority queue misses optimal order
def canJump(nums):
maxReach = 0
for i, jump in enumerate(nums):
# Bug: missing check if current index is beyond maxReach
maxReach = max(maxReach, i + jump)
if maxReach >= len(nums) - 1:
return True
return False
Solution
Step 1: Understand the missing condition
The code does not check if the current index i is beyond maxReach, which means it may continue even when stuck.Step 2: Identify the bug line
Line 4 updates maxReach without verifying if i is reachable, causing false positives on inputs with unreachable indices.Final Answer:
Option C -> Option CQuick Check:
Adding "if i > maxReach: return False" fixes the bug [OK]
- Forgetting to check i > maxReach
- Assuming maxReach update alone suffices
Solution
Step 1: Recognize negatives affect ordering and concatenation semantics
Negative numbers cannot be treated the same as positives because concatenation with '-' changes lex order.Step 2: Separate positives and negatives, sort positives with original comparator, sort negatives by absolute value descending
Concatenate positives first (largest number), then negatives to maintain largest overall concatenation.Step 3: This approach preserves ordering logic and handles negatives correctly
Other options either ignore negatives or mishandle signs causing incorrect results.Final Answer:
Option C -> Option CQuick Check:
Separating and sorting by sign handles negatives correctly [OK]
- Treating negatives as strings directly
- Ignoring negatives
- Converting negatives to positives incorrectly
Solution
Step 1: Understand unlimited supply impact
With infinite boxes, the best strategy is to fill the truck entirely with the box type having the highest units per box.Step 2: Identify correct algorithm
Sorting descending by units per box and taking all truckSize boxes from the top box type yields maximum units efficiently.Final Answer:
Option C -> Option CQuick Check:
Greedy fill with highest unit box type maximizes units [OK]
- Not reducing truckSize leading to infinite loop
- Using DP unnecessarily
- Sorting ascending which is suboptimal
