545. Top k Largest Numbers II
2019-07-08 本文已影响0人
鸭蛋蛋_8441
Description
Implement a data structure, provide two interfaces:
add(number). Add a new number in the data structure.
topk(). Return the top k largest numbers in this data structure. k is given when we create the data structure.
思路:
各种不同方法的时间复杂度见代码注释,具体用哪种数据结构取决于哪个操作用的比较多。min heap的优势在于永远使得heap中的元素为最大的k个,这样需要返回topk时只需要对数组降序排序就可以。
代码:
