Practice
from typing import List
def employeeFreeTime(schedule: List[List[List[int]]]) -> List[List[int]]:
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
schedule = [[[1,2],[5,6]], [[1,3]], [[4,10]]]
print(employeeFreeTime(schedule))
Solution
Step 1: Sort all intervals
Flattened intervals: [1,2], [5,6], [1,3], [4,10]. Sorted by start: [1,2], [1,3], [4,10], [5,6].Step 2: Merge intervals and find gaps
Initialize prev_end=2. Iterate: - Interval [1,3]: start=1 ≤ prev_end=2, update prev_end=max(2,3)=3 - Interval [4,10]: start=4 > prev_end=3, gap [3,4] added, update prev_end=10 - Interval [5,6]: start=5 ≤ prev_end=10, update prev_end=max(10,6)=10 Return free_times=[[3,4]].Final Answer:
Option D -> Option DQuick Check:
Only one gap between merged intervals is [3,4] [OK]
- Off-by-one errors in gap detection
- Including zero-length intervals
- Incorrect sorting leading to wrong gaps
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: Analyze heap operations per element
Each element is pushed once initially, O(n log n). Then max elements are halved repeatedly, up to log M times.Step 2: Combine heap operations and halving steps
Each halving involves a pop and push on heap, each O(log n), repeated up to log M times per element, total O(n log n * log M).Final Answer:
Option D -> Option DQuick Check:
Complexity accounts for heap operations and repeated halving [OK]
- Ignoring the log M factor from repeated halving
- Confusing n log n with n log M
- Assuming quadratic complexity due to nested loops
Solution
Step 1: Analyze balance() function
When moving elements between heaps, signs must be handled carefully to maintain max-heap and min-heap properties.Step 2: Identify incorrect sign usage
In line pushing to large heap, negation is incorrect; it should push positive val to min-heap, not negative.Final Answer:
Option D -> Option DQuick Check:
Incorrect sign breaks heap order, causing wrong median [OK]
- Incorrect sign negation when balancing heaps
- Forgetting to prune delayed elements
- Wrong median calculation for even k
n tasks and cooldown space?Solution
Step 1: Identify operations per task
Each task is processed once in a single loop over the tasks array.Step 2: Analyze hash map operations
Hash map lookups and updates for next_available are O(1) on average, so no nested loops or sorting involved.Final Answer:
Option B -> Option BQuick Check:
Single pass with O(1) lookups -> O(n) time -> [OK]
- Assuming cooldown causes nested loops increasing complexity
- Confusing with heap-based solutions that are O(n log n)
- Thinking space affects time complexity linearly
