How to find k nearest neighbors to the median of n distinct numbers in O(n) time?

后端 未结 13 1703
别跟我提以往
别跟我提以往 2021-02-02 16:26

I can use the median of medians selection algorithm to find the median in O(n). Also, I know that after the algorithm is done, all the elements to the left of the median are les

13条回答
  •  南旧
    南旧 (楼主)
    2021-02-02 17:04

    1. Find the median in O(n). 2. create a new array, each element is the absolute value of the original value subtract the median 3. Find the kth smallest number in O(n) 4. The desired values are the elements whose absolute difference with the median is less than or equal to the kth smallest number in the new array.

提交回复
热议问题