378. Kth Smallest Element in a Sorted Matrix
solution
import heapq
class Solution:
def kthSmallest(self, matrix: List[List[int]], k: int) -> int:
if not matrix or not matrix[0]:
return -1
heap = []
for i in range(len(matrix)):
for j in range(len(matrix[0])):
heapq.heappush(heap, matrix[i][j])
for _ in range(k):
res = heapq.heappop(heap)
return resLast updated