mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 12:16:14 -04:00
24 lines
503 B
Python
24 lines
503 B
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
# author: bt3gl
|
|
|
|
|
|
|
|
def kth_smallest(matrix, k) -> int:
|
|
|
|
min_heap = []
|
|
|
|
for row in range(min(k, len(matrix))):
|
|
min_heap.append((matrix[row][0], row, 0))
|
|
|
|
heapq.heapify(min_heap)
|
|
|
|
while k:
|
|
|
|
element, row, col = heapq.heappop(min_heap)
|
|
if col < len(matrix) - 1:
|
|
heapq.heappush(min_heap, (matrix[row][cow + 1], row, col + 1))
|
|
k -= 1
|
|
|
|
return element
|