File: root - text - article - 2020 - 02 - top-k-frequent-words.txt
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Category: Computing | 571 Views, 23373 Search Bots | 102 Words
| Browse | Archive
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Category: Computing | 571 Views, 23373 Search Bots | 102 Words
| Browse | Archive
Hi, here's your problem today. This problem was recently asked by AirBNB:
Given a non-empty list of words, return the k most frequent words. The output should be sorted from highest to lowest frequency, and if two words have the same frequency, the word with lower alphabetical order comes first. Input will contain only lower-case letters.
Example:
Input: ["daily", "interview", "pro", "pro", "for", "daily", "pro", "problems"], k = 2
Output: ["pro", "daily"]
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Cateogry: Computing | 571 Views, 23373 Search Bots | 102 Words Given a non-empty list of words, return the k most frequent words. The output should be sorted from highest to lowest frequency, and if two words have the same frequency, the word with lower alphabetical order comes first. Input will contain only lower-case letters.
Example:
Input: ["daily", "interview", "pro", "pro", "for", "daily", "pro", "problems"], k = 2
Output: ["pro", "daily"]
class Solution(object):
def topKFrequent(self, words, k):
# Fill this in.
words = ["daily", "interview", "pro", "pro", "for", "daily", "pro", "problems"]
k = 2
print(Solution().topKFrequent(words, k))
# ['pro', 'daily']
Related Articles
- Daily Interview Problem: Count Number of Unival Subtrees
- Find the non-duplicate number
- Daily Interview Puzzle: Minimum Size Subarray Sum
- Generate All IP Addresses
- Autorun.inf Virus Protection
- ImageMagick Vulnerabilities - CVE-2016–3714
- Daily Interview Problem: Full Binary Tree
- Algorithm Interview: Lowest Common Ancestor of 2 Nodes in Binary Tree
- Daily Interview Problem: Buddy Strings
- Algorithm Interview: No Adjacent Repeating Characters
©2006~2024 SteakOverCooked - 0.00682 Seconds(s) - 2898.315 KB/s - 11 Online Memory: 494.28 KB
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - Server PHP Version: 7.4.33
How to Cook a Perfect Steak? | <meta name="robots" content="index, follow">
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - Server PHP Version: 7.4.33
Comments (0)
Read & Write - Normal - Mini - Post - All Comments - Statistics
Be the first one to comment this page !