378 Kth Smallest Element in a Sorted Matrix
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.
matrix = [
[ 1, 5, 9],
[10, 11, 13],
[12, 13, 15]
k = 8,
You may assume k is always valid, 1 ≤ k ≤ n2.
similar to Merge K sorted Array/ List , use PriorityQueue。
Because the element of the Array cannot find the next directly, a class Node is used to store the value, x, y positions.
Initial O(n) time, also find k O(k), sort O(logn) => O(n + klogn)
variation: Kth Largest in N Arrays
we know where the boundary is start/end are the min/max value.
locate the kth smallest item (x, y) by cutt off partition in binary fasion:
find mid-value, and count # of items < mid-value based on the ascending matrix