Selection algorithms on sorted matrix

后端 未结 8 1772
野性不改
野性不改 2020-11-28 22:51

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

8条回答
  •  無奈伤痛
    2020-11-28 23:13

    You can find the kth smallest element in time O(n log n) expected, if you notice that:

    1. Generating a random number that lies between Array[i][j] and Array[k][l] such that Array[i][j] < Array[k][l] takes O(n) time (expected) and

    Using [1] as a subroutine, you can use a procedure similar to RANDOMIZED-SELECT to generate the kth smallest number in the whole array.

提交回复
热议问题