Jan 3, 2009

Given a n*m matrix having numbers such that each row and each column sorted. Now print the numbers in present in the matrix in sorted order.. ps: He g

Given a n*m matrix having numbers such that each row and each column sorted. Now print the numbers in present in the matrix in sorted order..
ps: He gave me the hint as, we can rearrange the matrix.
1 3 7
2 4 8
9 10 13

put all first elements of the each row at min heap, heapify the min heap, get the root, insert the root’s row’s next node, heapify the min heap, get the root, repeat this operation until all elements in the matrix are sorted
O(nmlogn) where n is row, m is column

No comments:

Post a Comment