Practice
def can_ship_early_stop(weights, days, capacity):
day_count = 1
current_load = 0
for w in weights:
if current_load + w > capacity:
day_count += 1
if day_count > days:
return False
current_load = 0
current_load += w
return True
def ship_with_capacity_binary_search_early_stop(weights, days):
left, right = max(weights), sum(weights)
while left < right:
mid = (left + right) // 2
if can_ship_early_stop(weights, days, mid):
right = mid
else:
left = mid + 1
return left
print(ship_with_capacity_binary_search_early_stop([3,2,2,4,1,4], 3))Solution
Step 1: Trace feasibility check with mid=6
Check if capacity=6 can ship in 3 days: Day 1: 3+2=5 (ok), next 2 would exceed 6, so day 2 starts Day 2: 2+4=6 (ok), next 1 would exceed 6, so day 3 starts Day 3: 1+4=5 (ok) All packages shipped in 3 days -> feasible.Step 2: Binary search narrows capacity
Try mid=5 (less than max weight 4, so invalid), mid=7 is next check, which is feasible. The minimal feasible capacity is 7.Final Answer:
Option A -> Option AQuick Check:
Minimal capacity found by binary search is 7 [OK]
- Off-by-one in mid calculation
- Miscounting days in feasibility
- Forgetting to reset current_load
m = 3, n = 3, and k = 5?Solution
Step 1: Trace countLessEqual for mid=4
For mid=4, count = sum of min(3, 4//i): i=1 -> min(3,4)=3; i=2 -> min(3,2)=2; i=3 -> min(3,1)=1; total=6 ≥ k=5, so high=4.Step 2: Trace countLessEqual for mid=3
For mid=3, count = i=1 -> 3; i=2 -> 1; i=3 -> 1; total=5 ≥ k=5, so high=3.Step 3: Trace countLessEqual for mid=2
For mid=2, count = i=1 -> 2; i=2 -> 1; i=3 -> 0; total=3 < k=5, so low=3.Final Answer:
Option D -> Option DQuick Check:
Output matches expected k-th smallest number 5 [OK]
- Off-by-one in counting
- Not breaking early in countLessEqual
Solution
Step 1: Understand problem constraints
The array is sorted, so searching can be optimized by halving the search space instead of scanning linearly.Step 2: Identify the algorithm that efficiently finds insertion index
Binary search divides the array repeatedly, guaranteeing O(log n) time, which is optimal compared to linear or DP approaches.Final Answer:
Option D -> Option DQuick Check:
Binary search is the classic approach for sorted array search problems [OK]
- Assuming greedy insertion is always optimal
- Confusing DP with binary search
- Using linear scan despite sorted input
from math import ceil
def can_arrive(dist, hour, speed):
total_time = 0.0
for i in range(len(dist)):
total_time += ceil(dist[i] / speed)
total_time -= ceil(dist[-1] / speed) # Adjust last segment
total_time += dist[-1] / speed
return total_time <= hour
Solution
Step 1: Analyze loop bounds
The loop iterates over all segments including the last, adding ceil for all, which overestimates time for last segment.Step 2: Identify correct approach
Only the first n-1 segments should use ceil; the last segment should add exact division time without ceiling.Final Answer:
Option C -> Option CQuick Check:
Loop should be for i in range(len(dist) - 1) [OK]
- Applying ceil to last segment
- Incorrect loop range including last segment
Solution
Step 1: Understand the reuse scenario
All trips must be completed at the same speed, so feasibility must be checked collectively.Step 2: Adapt binary search feasibility check
Perform binary search on speed and verify if all trips can be completed on time at that speed combined.Step 3: Reject separate searches or brute force
Separate searches ignore the single speed constraint; brute force is inefficient; DP is unnecessary.Final Answer:
Option B -> Option BQuick Check:
Single binary search with combined feasibility check handles reuse [OK]
- Binary searching each trip separately
- Switching to brute force unnecessarily
