373. Find K Pairs with Smallest Sums

373. Find K Pairs with Smallest Sums

from heapq import *

class Solution:
    def kSmallestPairs(self, nums1: List[int], nums2: List[int], k: int) -> List[List[int]]:
        heap=[(nums1[0]+nums2[0],0,0)]
        res=[]
        for i in range(k):
            if not heap:break
            _,p1,p2=heappop(heap)
            res.append([nums1[p1],nums2[p2]])

            if p2+1

你把这个想像成一个矩阵

p1 row, p2 col

你可能感兴趣的:(leetcode)