692 Top K Frequent Words
692. Top K Frequent Words
1. Question
Given a non-empty list of words, return thekmost 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:
Example 2:
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(nlogk) time and O(n) extra space.
2. Implementation
(1) Trie + PriorityQueue
3. Time & Space Complexity
Trie + PriorityQueue: 时间复杂度O(nlogk), n是word的个数,空间复杂度O(n)
Last updated
Was this helpful?