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

前端 未结 30 2192
深忆病人
深忆病人 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 15:03

    You can iterate over the numbers which takes O(n)

    Whenever you find a value greater than the current minimum, add the new value to a circular queue with size 100.

    The min of that circular queue is your new comparison value. Keep on adding to that queue. If full, extract the minimum from the queue.

提交回复
热议问题