D***0 发帖数: 138 | 1 Write a function that takes two parameters: (1) a String representing a text
document and (2) an integer providing the number of items to return.
Implement the function such that it returns a list of Strings ordered by
word frequency, the most frequently occurring word first.Your solution
should run in O(n) time where n is the number of characters in the document.
这个是不是就是设置个大小为k的min-heap,先扫一遍统计每个word的frequency,放到
hashmap中,然后在扫一遍hashmap,然后放到min-heap中。那这种做法的复杂度是O(n
+ mlogk + klogk)是吧,也就是要求的O(n),因为k是输入的,m是word的个数。谢谢。 |
|