Finding the first n largest elements in an array

后端 未结 8 463
不思量自难忘°
不思量自难忘° 2020-12-09 04:42

I have got an array containing unique elements. I need to find out the first n largest elements in the array in the least complexity possible. The solution that I could thin

8条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-09 05:17

    Find the kth biggest element, using selection algorithm.
    Next, iterate the array and find all elements which are larger/equal it.

    complexity: O(n) for selection and O(n) for iterating, so the total is also O(n)

提交回复
热议问题