Practice
k in an integer array efficiently. Which approach best guarantees an optimal solution for this problem?Solution
Step 1: Understand the problem constraints
Finding median in each sliding window requires efficient insertion, deletion, and median retrieval.Step 2: Evaluate approaches
Sorting each window (A) is too slow (O(nk log k)). DP (C) doesn't apply well to median. Simple queue (D) can't maintain order efficiently. Balanced two heaps (B) maintain order and allow O(log k) updates.Final Answer:
Option C -> Option CQuick Check:
Two heaps maintain median efficiently [OK]
- Thinking sorting each window is efficient enough
- Assuming DP can precompute medians
- Using simple queue without order maintenance
Solution
Step 1: Understand problem requirements
The problem requires finding, for each interval, the next interval with the smallest start time ≥ current interval's end time.Step 2: Evaluate approaches
Brute force (nested loops) is correct but inefficient (O(n²)). Sorting + binary search is efficient but requires extra steps. The two max-heaps approach efficiently tracks max starts and ends to find next intervals in O(n log n) time, guaranteeing optimality.Final Answer:
Option A -> Option AQuick Check:
Two heaps approach matches the problem's need for efficient next interval lookup [OK]
- Assuming greedy earliest finish works
- Using DP unnecessarily
- Brute force accepted as optimal
result after processing nums = [1, 3, -1, -3] with k = 3?Solution
Step 1: Trace first window [1, 3, -1]
Sorted window is [-1, 1, 3], median is middle element 1.0.Step 2: Trace second window [3, -1, -3]
Sorted window is [-3, -1, 3], median is middle element -1.0.Final Answer:
Option A -> Option AQuick Check:
Two medians computed correctly for windows of size 3 [OK]
- Off-by-one error in window slicing
- Incorrect median index for odd k
- Confusing median calculation for even k
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
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
