378. Kth Smallest Element in a Sorted Matrix

# Medium

Because we can't find the next step should go down or right according to column and row ascending principle, the focus of this problem is not about the matrix, it is about sorting. In an ascending array, return a[k-1].

class Solution:
    def kthSmallest(self, matrix: List[List[int]], k: int) -> int:
        res = []
        for i in range(len(matrix)):
            for j in range(len(matrix[i])):
                res.append(matrix[i][j])
                
        res.sort()
        return res[k-1]

Last updated