Question DSA-HEAP-0064
HardWhat is the output of this code?
import heapq
def kth_matrix(m, k):
h = [(row[0], i, 0) for i, row in enumerate(m)]
heapq.heapify(h)
for _ in range(k - 1):
v, i, j = heapq.heappop(h)
if j + 1 < len(m[i]): heapq.heappush(h, (m[i][j + 1], i, j + 1))
return h[0][0]
print(kth_matrix([[1, 5, 9], [10, 11, 13], [12, 13, 15]], 8))
- A.
13
- B.
15
- C.
Sign up free to see the answer and a step-by-step explanation.