Problem
Given a list of words and a number k, return the `k` most frequent words. Order the result from most frequent to least frequent, and break ties alphabetically (the word that comes first in dictionary order goes first).
Example 1
- Input
- words = ["i", "love", "code", "i", "love", "tea"], k = 2
- Output
- ["i", "love"]
Example 2
- Input
- words = ["cat", "bat", "cat", "bat", "ant"], k = 2
- Output
- ["bat", "cat"]
In the second example bat and cat both appear twice, and bat comes first alphabetically. Can you do it in O(n log k)?
Constraints
1 <= words.length <= 5001 <= words[i].length <= 10words[i] holds only lowercase English letters.1 <= k <= the number of distinct words.