← 返回 amazon 的题目列表Top K Frequent Words in a File
类型:online_judge
Given a file with multiple words, implement a function to return the top K frequent words.
Each line of the file contains several words separated by spaces. You need to:
Analyze the frequency of these words.
Return the top K frequent words in descending order by frequency; if frequencies are the same, return them in lexicographical order.
Input
An integer K, indicating the number of top frequent words to return.
A file where each line contains several words.
Output
A list of the top K frequent words.
Example
File content:
hello world
hello
leetcode leetcode hello
Input: K = 2
Output: ["hello", "leetcode"]
Example
Input
2
hello world
hello
leetcode leetcode hello