Practice
Solution
Step 1: Identify heap initialization
Projects are pushed into min_capital_heap without sorting by capital, which is required for efficient affordability checks.Step 2: Understand impact
Without sorting, min_capital_heap may not have the smallest capital project at the top, breaking the affordability filtering logic.Final Answer:
Option A -> Option AQuick Check:
Min-heap must be built from sorted projects or pushed in sorted order [OK]
- Assuming heapq.heappush sorts automatically
- Confusing max-heap and min-heap roles
- Ignoring early stopping condition
import heapq
def find_next_intervals(intervals):
n = len(intervals)
result = [-1] * n
max_start_heap = []
max_end_heap = []
for i, (start, end) in enumerate(intervals):
heapq.heappush(max_start_heap, (start, i)) # Line 9
heapq.heappush(max_end_heap, (-end, i))
while max_end_heap:
end, end_idx = heapq.heappop(max_end_heap)
end = -end
if not max_start_heap or -max_start_heap[0][0] < end:
continue
candidates = []
while max_start_heap and -max_start_heap[0][0] >= end:
start, start_idx = heapq.heappop(max_start_heap)
candidates.append((start, start_idx))
min_start, min_idx = max(candidates)
result[end_idx] = min_idx
for c in candidates:
if c != (min_start, min_idx):
heapq.heappush(max_start_heap, c)
return result
Solution
Step 1: Identify heap usage
max_start_heap must be a max-heap by pushing (-start, i) to pop intervals with largest start first.Step 2: Analyze Line 9
Line 9 pushes (start, i) without negation, making it a min-heap, breaking the logic that relies on max-heap ordering.Final Answer:
Option B -> Option BQuick Check:
Using min-heap here causes incorrect candidate selection [OK]
- Forgetting to negate start for max-heap
- Misusing min-heap for max-heap logic
- Incorrect candidate selection due to heap order
n and window size k?Solution
Step 1: Identify operations per element
Each element is inserted and removed once from heaps, each operation O(log k).Step 2: Calculate total complexity
For n elements, total time is O(n log k), better than brute force O(n k log k).Final Answer:
Option A -> Option AQuick Check:
Logarithmic heap operations per element yield O(n log k) [OK]
- Confusing k with n in complexity
- Assuming sorting each window is optimal
- Ignoring heap balancing cost
def task_scheduler_buggy(tasks, space):
next_available = {}
day = 1
for t in tasks:
if t in next_available:
day = next_available[t] # Bug here
next_available[t] = day + space + 1
day += 1
return day - 1
Solution
Step 1: Analyze day update logic
Line 6 assigns day = next_available[t], which can move day backwards if day is already ahead, violating cooldown constraints.Step 2: Correct approach
Use day = max(day, next_available[t]) to ensure day never moves backward and respects cooldown.Final Answer:
Option A -> Option AQuick Check:
Assignment without max causes scheduling too early -> [OK]
- Assigning day directly instead of max causes cooldown violation
- Misplacing day increment causing off-by-one errors
- Incorrect next_available update formula
Solution
Step 1: Understand reuse implication
Allowing reuse means projects remain available after selection, so they must be reinserted to be considered again.Step 2: Modify algorithm accordingly
After popping a project from min-capital heap and selecting it, reinsert it to allow future reuse; otherwise, it disappears from consideration.Final Answer:
Option C -> Option CQuick Check:
Reinserting projects enables multiple selections [OK]
- Ignoring reinsertion, losing projects after first pick
- Keeping all projects always available breaks capital filtering
- Ignoring capital constraints when reusing projects
