forked from SamirPaulb/DSAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create 07. Find K Pairs with Smallest Sums.py
- Loading branch information
1 parent
21abc87
commit bba2735
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# https://leetcode.com/problems/find-k-pairs-with-smallest-sums/ | ||
|
||
class Solution: | ||
def kSmallestPairs(self, nums1: List[int], nums2: List[int], k: int) -> List[List[int]]: | ||
# we use maxHeap so that we can pop largest number among k+1 numbers in maxHeap first | ||
maxHeap = [] | ||
|
||
""" | ||
instead of iterating over all the numbers of both array, we can iterate only | ||
the first 'K' numbers from both array. | ||
Since they are sorted in ascending order, the pairs with the minimum sum will | ||
be just the first 'K' numbers from those two arrays. | ||
""" | ||
for i in range(0, min(k, len(nums1))): | ||
for j in range(0, min(k, len(nums2))): | ||
x = nums1[i] | ||
y = nums2[j] | ||
|
||
# sum of two number | ||
total = x + y | ||
|
||
if len(maxHeap) < k: | ||
heapq.heappush(maxHeap, [-total, x, y]) | ||
else: | ||
# if the sum of x and y is larger than the largest (among the k smallests) | ||
# sum, we can 'break' here. Since the arrays are sorted in the ascending order, | ||
# we will not be able to find a pair with smaller sum moving forward. | ||
if total > -maxHeap[0][0]: | ||
break | ||
|
||
# push new numbers to the heap | ||
heapq.heappush(maxHeap, [-total, x, y]) | ||
|
||
# pop the largest number among k+1 numbers in maxHeap, so that only | ||
# k smallest numbers are in maxHeap | ||
heapq.heappop(maxHeap) | ||
|
||
result = [] | ||
while maxHeap: | ||
popped = heapq.heappop(maxHeap) | ||
result.append([popped[1], popped[2]]) | ||
|
||
return result | ||
|