Kth small element in matrix
question:
1) matrix with sorted rows (m) and columns(n):
10, 20, 30, 40 15, 25, 35, 45 24, 29, 37, 48 32, 33, 39, 50
解法:
Create an array of n.
1. initialized it with the first row.
2. Get the first one (i,j) in this array, which is the smallest one in the rest nodes. Replace it(i,j) with the node of its next row (i+1,j).
3. insert (i+1,j) in the correct position in the array to maintain the sorting.
4. Keep doing 2 and 3 until k nodes are retrieved.
klog(m)
2) matrix with unsorted rows (m) and columns (n):
use quick selection to find the kth element (similar method to 1-D array)
O(m*n)
浙公网安备 33010602011771号