Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
Mohammed Dh Abbas
incorrect solution and test cases

Mohammed Dh Abbas

May 22, 2024

Since the arrays are sorted the this implies that we are interested in the max value of one of the arrays and some value form another array

e.g:

A = [5, 2, 1]

B = [31, 4, 2]

meaning we are comparing the sum [5 + 31] vs [5 + 4] vs [5 + 2] from A

AND

[31 + 2] vs [31 + 1] from B

def findKLargestPairs(self, nums1, nums2, k): result = [] max1 = nums1[0] max2 = nums2[0] max_heap = [] for i in range(0, len(nums1)): heappush(max_heap, (-(max2 + nums1[i]), [max2, nums1[i]])) for i in range(1, len(nums2)): # starting from 1 we dont want repeat the value of [max1, max2] heappush(max_heap, (-(max1 + nums2[i]), [max1, nums2[i]])) for _ in range(k): add, array = heappop(max_heap) result.append(array) return result

0

0

Comments
Comments

On this page