Practice
k pairs with the smallest sums formed by taking one element from each array. Which approach guarantees an optimal solution with efficient time complexity?Solution
Step 1: Understand problem constraints
The problem requires finding the k smallest pairs by sum from two sorted arrays efficiently.Step 2: Evaluate approaches
Brute force enumerates all pairs, which is inefficient. DP is not suitable since pairs are not independent subproblems. Greedy fails because pairing smallest elements once misses other smaller sums. The min-heap approach lazily expands pairs starting from the smallest sums, ensuring optimality and efficiency.Final Answer:
Option C -> Option CQuick Check:
Min-heap approach matches the known optimal solution [OK]
- Assuming greedy pairing always works
- Thinking DP applies directly
- Believing brute force is efficient enough
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:
low = mid + 1
else:
high = mid
return low
print(kthSmallestSum(nums1, nums2, k))Solution
Step 1: Trace binary search bounds
low=3 (1+2), high=17 (11+6). Mid values checked: 10, 6, 4, 3, 7.Step 2: Count pairs ≤ mid
For mid=7, countPairs returns 3 pairs ≤7, which matches k=3, so high=7.Final Answer:
Option B -> Option BQuick Check:
Output matches expected k-th smallest sum [OK]
- Off-by-one in binary search bounds
- Miscounting pairs in countPairs
nums = [[4,10,15,24,26],[0,9,12,20],[5,18,22,30]], what is the final returned range?Solution
Step 1: Initialize heap and current_max
Heap contains (4,0,0), (0,1,0), (5,2,0); current_max = 5.Step 2: Iterate popping min and pushing next elements
Pop (0,1,0), push (9,1,1), current_max=9; range = 9-0=9. Pop (4,0,0), push (10,0,1), current_max=10; range=10-4=6. Pop (5,2,0), push (18,2,1), current_max=18; range=18-5=13. Pop (9,1,1), push (12,1,2), current_max=18; range=18-9=9. Pop (10,0,1), push (15,0,2), current_max=18; range=18-10=8. Pop (12,1,2), push (20,1,3), current_max=20; range=20-12=8. Pop (15,0,2), push (24,0,3), current_max=24; range=24-15=9. Pop (18,2,1), push (22,2,2), current_max=24; range=24-18=6. Pop (20,1,3), no next element, break.Final Answer:
Option D -> Option DQuick Check:
Final minimal range is [4,20] covering all lists [OK]
- Forgetting to update current_max after pushing new element
- Stopping too early before all lists exhausted
- Miscomputing range after each pop
Solution
Step 1: Identify the role of current_max
current_max tracks the maximum element in the current window; it must be updated when pushing a new element.Step 2: Locate missing update
The code comment indicates current_max is not updated after pushing next_val, causing incorrect range calculation.Final Answer:
Option C -> Option CQuick Check:
Without updating current_max, range may be underestimated or incorrect [OK]
- Forgetting to update current_max after pushing new element
- Breaking loop too early or too late
- Misusing heap pop or initialization
Solution
Step 1: Understand reuse effect on counting
With reuse allowed, pairs are not limited to unique indices; counting must consider all j for each i.Step 2: Modify countPairs
Resetting j for each i allows counting all pairs with sums ≤ mid including repeated elements.Final Answer:
Option C -> Option CQuick Check:
Resetting j ensures correct counting with reuse [OK]
- Assuming original countPairs works with reuse
- Using heap without visited set causes duplicates
