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

0% completed

Vote For New Content
Why not using the same heap approach?

Baraa Attabbaa

Sep 23, 2023

result = [] h = [] heappush(h, (-(nums1[0]+nums2[0]), 0, 0)) for i in range(k): if not h: break v, i1, i2 = heappop(h) result.append([nums1[i1], nums2[i2]]) if i2 < len(nums2) - 1: heappush(h, (-(nums1[i1]+nums2[i2+1]), i1, i2+1)) if i1 < len(nums1) - 1: heappush(h, (-(nums1[i1+1]+nums2[i2]), i1+1, i2)) # TODO: Write your code here return result

0

0

Comments
Comments
B
Baraa Attabbaa2 years ago

if there are repeated values then it can be solved with a hashmap of tuples

On this page