Kth Smallest in a Sorted Matrix
Merge the sorted rows with a heap and stop at the kth value.
The problem
Implement Solution.kthSmallest(matrix, k). matrix is an n x n grid of integers where every row and every column is sorted in ascending order. Return the kth smallest value in the whole matrix as an int. Equal values count as separate positions: in [[1, 2], [1, 3]] the 1st and 2nd smallest are both 1. n is between 1 and 300, and 1 <= k <= n * n. Try to use less than O(n * n) extra memory.
Starter code
class Solution:
def kthSmallest(self, matrix, k):
passPublic examples
matrix = [[1, 5, 9], [10, 11, 13], [12, 13, 15]], k = 8gives13matrix = [[1, 2], [10, 20]], k = 2gives2matrix = [[3, 7], [5, 9]], k = 4gives9
Solving it in the practice book needs an active paid plan.
Open it in the practice bookOne thought at a time.
