算法:Top K frequent word

给定一个包含不同单词的文本,返回出现频率最高的前K个单词。当K大于单词的种类数时,返回所有不同的单词,按频率降序排列。文章提出了计算每个单词频率并使用最小堆来维护Top K高频词的思路。
摘要由CSDN通过智能技术生成

Given a composition with different kinds of words, return a list of the top K most frequent words in the composition.
Assumptions
the composition is not null and is not guaranteed to be sorted
K >= 1 and K could be larger than the number of distinct words in the composition, in this case, just return all the distinct words
Return
a list of words ordered from most frequent one to least frequent one (the list could be of size K or smaller than K)
Examples
Composition = [“a”, “a”, “b”, “b”, “b”, “b”, “c”, “c”, “c”, “d”], top 2 frequent words are [“b”, “c”]
Composition = [“a”, “a”, “b”, “b”, “b”, “b”, “c”, “c”, “c”, “d”], top 4 frequent words are [“b”, “c”, “a”, “d”]
Composition = [“a”, “a”, “b”, “b”, “b”, “b”, “c”, “c”, “c”, “d”], top 5 frequent words are [“b”, “c”, “a”, “d”]

Ideas
Calculate frequencies of each word and sto

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值