692. Top K Frequent Words
Problem description:
Given a non-empty list of words, return the k most frequent elements.
Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, then the word with the lower alphabetical order comes first.
Example 1:1
2
3
4Input: ["i", "love", "leetcode", "i", "love", "coding"], k = 2
Output: ["i", "love"]
Explanation: "i" and "love" are the two most frequent words.
Note that "i" comes before "love" due to a lower alphabetical order.
Example 2:1
2
3
4Input: ["the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"], k = 4
Output: ["the", "is", "sunny", "day"]
Explanation: "the", "is", "sunny" and "day" are the four most frequent words,
with the number of occurrence being 4, 3, 2 and 1 respectively.
Note:
You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
Input words contain only lowercase letters.
Follow up:
Try to solve it in O(n log k) time and O(n) extra space.
Solution:
We can use map
and bucket sort
to solve. The reason to choose map
instead of unordered_map
is because of the alphabetical order.
- Count the appearance of each word
- Create buckets. Only need to create
words.size()
buckets.
1 | class Solution { |
Solution python heap
We could do without the count since by default the lower count will be popped first in a heapq implementation - the idea is to create a heap of length k containing the tuple (count,word)
. When length exceeds k
, the tuple with lower count is popped, thereby always having the top k
count words.
However, if count is same in 2 tuples, the alphabetically lower word is popped out since that is the default ordering in a heapq (minheap) implementation, i.e for (count, word1)
and (count, word2)
, if word1 < word2
, (count, word1)
is popped. We create a custom class to reverse this ordering such that if word1 < word2
, (count, word2)
is popped. Our heap will then have the top k
frequent words, with alphabetically lower words being treated as "greater"
when the count is same in two tuples.
1 | class ReverseWordOrder: |
Solution python Trie:
1 | class TrieNode: |
time complexity: $O(nlogn)$
space complexity: $O(n)$
reference:
https://goo.gl/Eznk8c
https://goo.gl/oMLd8T