python 6 lines. very similar to n-way merge algorithm


  • 0
    Z
    def kthSmallest(self, matrix, k):
        heap = [(matrix[0][i], 1, i) for i in range(len(matrix))]
        for _ in range(k - 1):
            _, i, j = heapq.heappop(heap)
            if i < len(matrix):
                heapq.heappush(heap, (matrix[i][j], i + 1, j))
        return heap[0][0]
    
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.