this is a google interview question :
Given a N*N Matrix. All rows are sorted, and all columns are sorted. Find the Kth Largest element of the matrix.
doing
You do a breath first search starting at the (0,0). (0,0)’s 2 children (0,1) & (1,0) are added to the potential candidates list for the 2nd element. Loop picking the smallest element in the potential candidates list to be the next element, add it’s children to the potential candidates list. Stop when find the kth element.
Make the potential candidates list a min heap. The heap will never be bigger than n+m.
Also you could do the reverse from the last element (n,m) if k is greater than n*m/2.
Worst Case: this would be n*m/2 lg(n + m), instead of n*m lg(n * m) of sorting.