Top K Frequent Words II

Data Stream, HashMap, TreeSet

Description

Find top_k _frequent words in realtime data stream.

Implement three methods for Topk Class:

  1. TopK(k). The constructor.

  2. add(word). Add a new word.

  3. topk(). Get the current top _k _frequent words.

If two words have the same frequency, rank them by alphabet.

Example

TopK(2)
add("lint")
add("code")
add("code")
topk()
>> ["code", "lint"]

Solution

Jiuzhang's Solution: HashMap + TreeSet

注意:

  1. 这里add()时,如果TreeSet中存在这个word,要先删去该word,更新HashMap中word对应的count,再将其加入TreeSet,这样就可以利用更新过的count来排序了。否则TreeSet中已经存在的word没法改变其排序。

  2. 这里的TreeSet用的是根据words count来排序,并且count越高排序越靠前,因此可以想成(用PriorityQueue)的Max-Heap,与PriorityQueue相对应的,这里TreeSet因为可以直接用pollLast()删去last(highest) element,也就是移除最小的word count,因此这里可以使用Max-Heap的思想而不是Min-Heap。

Last updated

Was this helpful?