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

前端 未结 30 2207
深忆病人
深忆病人 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条回答
  •  甜味超标
    2020-11-29 14:45

    An very easy solution would be to iterate through the array 100 times. Which is O(n).

    Each time you pull out the largest number (and change its value to the minimum value, so that you don't see it in the next iteration, or keep track of indexes of previous answers (by keeping track of indexes the original array can have multiple of the same number)). After 100 iterations, you have the 100 largest numbers.

提交回复
热议问题