378. Kth Smallest Element in a Sorted Matrix
Similar to #373: https://marcopolocai.github.io/2018/06/20/LeetCode-Challenge-log-373/.
However, because all we want is just the kth number, we can use a binary-search-like approach to make a guess and count if it has k-1 lower numbers. It is a faster approach but very tricky to implement, as you has to be very careful on the boundary.
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth smallest element in the matrix.
Note that it is the kth smallest element in the sorted order, not the kth distinct element.
Example:
1 | matrix = [ |
Note:
You may assume k is always valid, 1 ≤ k ≤ n2.
Soulution:
1 | # piority queue approach |
1 | # binary seach |