Write a program to find 100 largest numbers out of an array of 1 billion numbers

前端 未结 30 2205
深忆病人
深忆病人 2020-11-29 14:04

I recently attended an interview where I was asked \"write a program to find 100 largest numbers out of an array of 1 billion numbers.\"

I was only able to give a br

30条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-29 14:55

    1. Use nth-element to get the 100'th element O(n)
    2. Iterate the second time but only once and output every element that is greater than this specific element.

    Please note esp. the second step might be easy to compute in parallel! And it will also be efficiently when you need a million biggest elements.

提交回复
热议问题