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 | 547 Views, 22269 Search Bots | 102 Words
| Browse | Archive
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Category: Computing | 547 Views, 22269 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 | 547 Views, 22269 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: Merge K Sorted Linked Lists
- [Daily Problem] Longest Increasing Subsequence
- Invert a Binary Tree
- Daily Interview Problem: Running Median
- Algorithm Interview: String Compression
- YES!!
- Daily Interview Question: Edit Distance
- Daily Interview Question: Longest Sequence with Two Unique Numbers
- Reverse a Linked List
- Compare Version Numbers
©2006~2024 SteakOverCooked - 0.00873 Seconds(s) - 2230.61 KB/s - 41 Online Memory: 491.63 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="noindex, 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 !