参考代码:
class Solution:
def kthSmallestSum(self, A, B, k):
if not A or not B:
return None
n, m = len(A), len(B)
minheap = [A[0] + B[0], 0, 0]
visited = set([0])
num = None
for _ in range(k):
num, x, y = heapq.heappop(minheap)
if x+1 < n and (x + 1) * m + y not in visited:
heapq.heappush(minheap, (A[x + 1] + B[y], x + 1, y))
visited.add((x + 1) * m + y)
if y+1 < m and x * m + y + 1 not in visited:
heapq.heappush(minheap, (A[x] + B[y + 1], x, y+1))
visited.add(x * m + y + 1)
return num