Practice
K identical machines and a list of jobs, each with a start time and duration. You want to assign each job to a machine so that each machine processes jobs sequentially without overlap, always choosing the earliest available machine with the smallest index. Which algorithmic approach guarantees an optimal assignment minimizing waiting times and correctly handles job scheduling in chronological order?Solution
Step 1: Understand the problem constraints
The problem requires assigning jobs to machines so that each job starts no earlier than its start time, machines process jobs sequentially, and the earliest available machine with the smallest index is chosen.Step 2: Identify the correct approach
Using a min-heap to track machine availability by next free time allows efficient retrieval of the earliest available machine. Sorting jobs by start time ensures chronological processing. This approach guarantees optimal scheduling and respects machine indices.Final Answer:
Option B -> Option BQuick Check:
Min-heap + sorting jobs by start time -> optimal scheduling [OK]
- Using linear search for machine availability leads to O(nK) time
- Sorting by duration ignores job start times and causes conflicts
- DP approach is infeasible due to large state space
nth number whose prime factors only include 2, 3, and 5. Which approach guarantees an optimal solution that efficiently merges multiple sorted sequences generated by multiplying previously found numbers by these primes?Solution
Step 1: Understand the problem structure
The problem requires generating numbers whose prime factors are only 2, 3, and 5, which can be seen as merging three sorted sequences: multiples of 2, 3, and 5 of previously found ugly numbers.Step 2: Identify the optimal merging technique
A min-heap efficiently merges these sequences by always extracting the smallest next candidate and avoids duplicates by tracking seen numbers, ensuring optimal time complexity.Final Answer:
Option A -> Option AQuick Check:
Min-heap merges k sorted sequences optimally [OK]
- Assuming brute force is efficient enough
- Ignoring duplicates in merging
- Using greedy without tracking duplicates
def countPairs(nums1, nums2, mid):
count = 0
j = len(nums2) - 1
for i in range(len(nums1)):
while j >= 0 and nums1[i] + nums2[j] > mid:
j -= 1
count += (j + 1)
return count
def kthSmallestSum(nums1, nums2, k):
low = nums1[0] + nums2[0]
high = nums1[-1] + nums2[-1]
while low < high:
mid = (low + high) // 2
if countPairs(nums1, nums2, mid) <= k: # Bug here
low = mid + 1
else:
high = mid
return lowSolution
Step 1: Understand binary search condition
Correct condition is countPairs(...) < k to move low up; using ≤ k skips valid sums.Step 2: Effect of bug
Using ≤ k causes binary search to overshoot and miss the exact k-th sum, returning incorrect result.Final Answer:
Option D -> Option DQuick Check:
Changing ≤ to < fixes correctness [OK]
- Using ≤ instead of < in binary search condition
- Off-by-one errors in countPairs
Solution
Step 1: Identify number of merge rounds
Divide and conquer merges pairs of arrays, halving the number of arrays each round, so log k rounds.Step 2: Calculate work per round
Each round merges all N elements once, so O(N) per round.Final Answer:
Option B -> Option BQuick Check:
O(N) work per round x log k rounds -> O(N log k) [OK]
- Confusing total elements N with number of arrays k
- Assuming sorting entire array each time
- Multiplying N by k incorrectly
Solution
Step 1: Analyze heap initialization and loop
Heap is correctly initialized with first k+1 elements; loop pushes and pops correctly.Step 2: Check for extraction after loop
After the loop, remaining elements in heap are not popped and placed back into arr, causing incomplete output.Final Answer:
Option D -> Option DQuick Check:
Missing final extraction leads to missing sorted elements [OK]
- Forgetting to pop leftover heap elements
- Incorrect heap size initialization
- Misusing heap push/pop functions
