Practice
findMaxCapital(2, 0, [1, 2, 3], [0, 1, 1])?Solution
Step 1: Trace first iteration with W=0
Projects with capital ≤0: only (0,1). Move profit 1 to max heap. Pop max profit 1, W=0+1=1.Step 2: Trace second iteration with W=1
Projects with capital ≤1: (1,2) and (1,3). Move profits 2 and 3 to max heap. Pop max profit 3, W=1+3=4.Final Answer:
Option B -> Option BQuick Check:
Final capital after 2 projects is 4 [OK]
- Forgetting to push all affordable projects each iteration
- Off-by-one error in loop iterations
- Confusing min-heap and max-heap usage
Solution
Step 1: Understand problem constraints and operations
The problem allows doubling odd numbers once and halving even numbers multiple times to minimize deviation.Step 2: Identify approach that efficiently tracks max and min values
Using a max-heap allows quick access to the current maximum, which can be reduced by halving, while tracking the minimum seen so far.Final Answer:
Option A -> Option AQuick Check:
Max-heap approach efficiently updates max and min values [OK]
- Thinking sorting + two pointers always works despite operations changing values
- Assuming brute force is feasible for large inputs
- Believing DP is needed despite no overlapping subproblems
def employeeFreeTime(schedule):
intervals = [iv for employee in schedule for iv in employee]
intervals.sort(key=lambda x: x[0])
free_times = []
prev_end = intervals[0][1]
for i in range(1, len(intervals)):
start, end = intervals[i]
if start >= prev_end:
free_times.append([prev_end, start])
prev_end = max(prev_end, end)
return free_times
Solution
Step 1: Identify condition for free time
Free time exists only if start > prev_end, not when start == prev_end, because zero-length intervals are invalid.Step 2: Analyze buggy condition
The condition uses start >= prev_end, which incorrectly adds zero-length intervals when start == prev_end.Final Answer:
Option C -> Option CQuick Check:
Changing >= to > fixes zero-length interval bug [OK]
- Using >= instead of > for gap detection
- Not sorting intervals before merging
- Incorrectly updating prev_end
import heapq
def minimumDeviation(nums):
heap = []
min_val = float('inf')
for num in nums:
if num % 2 == 1:
num *= 2
min_val = min(min_val, num)
heapq.heappush(heap, num) # BUG: pushing positive num instead of negative
min_dev = float('inf')
while True:
max_val = -heapq.heappop(heap)
min_dev = min(min_dev, max_val - min_val)
if max_val % 2 == 0:
max_val //= 2
min_val = min(min_val, max_val)
heapq.heappush(heap, -max_val)
else:
break
return min_dev
Solution
Step 1: Identify heap usage
The algorithm uses a max-heap simulated by pushing negative values.Step 2: Locate bug in heap push
Line pushing positive num instead of negative breaks max-heap property, causing incorrect max_val extraction.Final Answer:
Option B -> Option BQuick Check:
Heap must store negative values to simulate max-heap [OK]
- Using min-heap without negation for max element
- Not updating min_val after halving
- Trying to halve odd numbers
Solution
Step 1: Identify decrement of count
The algorithm must decrement the count of the character after using it, i.e., count + 1 since counts are negative.Step 2: Locate missing decrement
In the buggy line, count is appended without incrementing, so the character count never decreases, causing infinite loops or invalid results.Final Answer:
Option C -> Option CQuick Check:
Correct decrement is critical to track usage [OK]
- Forgetting to decrement count after popping from heap
- Misusing queue length to enforce k-distance
- Incorrect heap operations or conditions
