如果是,top 10 trending words那么你应该使用max-heapa hash-table.
当一个新单词添加到该文件时,然后:
Createx带x.key=word和的新元素x.count=1.Add x到了hash-table.O(1).Add x到了max-heap.O(lgn).将现有单词添加到文件后,则:
Find x在hash-table.O(1).Update x.count到x.count++.当需要检索时top 10 trending words:
Extract10次起max-heap.10*O(lgn)=O(10*lgn)=O(lgn).如您所见,所有需要的操作最多都在完成O(lgn).
| 归档时间: |
|
| 查看次数: |
5158 次 |
| 最近记录: |