Top K Frequent Words

Given a list of words and an integer k, return the top k frequent words in the list.

Given

[
    "yes", "lint", "code",
    "yes", "code", "baby",
    "you", "baby", "chrome",
    "safari", "lint", "code",
    "body", "lint", "code"
]

for k = 3, return ["code", "lint", "baby"].

for k = 4, return ["code", "lint", "baby", "yes"],

You should order the words by the frequency of them in the return list, the most frequent one comes first. If two words has the same frequency, the one with lower alphabetical order come first.

这题是代码思路还是很明确的,先利用dict,统计每个字符出现的次数,之后进行排序,取前k个。但是注意提示中写的,两个单词出现频次相同时,按照词典序让比较小的排在前面。所以实际在写的时候,需要不仅靠频次排序,也需要靠词本身排序。代码如下:

class Solution:
    # @param {string[]} words a list of string
    # @param {int} k an integer
    # @return {string[]} a list of string
    def topKFrequentWords(self, words, k):
        map = {}
        for w in words:
            if w not in map:
                map[w] = 1
            else:
                map[w] += 1
        import heapq
        heap = []
        for key, value in map.iteritems():
            heapq.heappush(heap,(0-value,key))

        res = [w[1] for w in heapq.nsmallest(k,heap,key = lambda x: (x[0],x[1]))]
        return res
时间: 2024-10-05 16:26:04

Top K Frequent Words的相关文章

347. Top K Frequent Elements

Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2,2,3] and k = 2, return [1,2]. Note: 347. Top K Frequent ElementsYou may assume k is always valid, 1 ≤ k ≤ number of unique elements. Your algorithm's

LeetCode OJ 347. Top K Frequent Elements hashmap+排序求解

题目链接:https://leetcode.com/problems/top-k-frequent-elements/. 347. Top K Frequent Elements My Submissions QuestionEditorial Solution Total Accepted: 15510 Total Submissions: 36453 Difficulty: Medium Given a non-empty array of integers, return the k mo

[LeetCode][Python]Top K Frequent Elements

op K Frequent Elements Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2,2,3] and k = 2, return [1,2]. Note: You may assume k is always valid, 1 ≤ k ≤ number of unique elements. Your algorithm's time

[LeetCode] Top K Frequent Elements 前K个高频元素

Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2,2,3] and k = 2, return [1,2]. Note: You may assume k is always valid, 1 ≤ k ≤ number of unique elements.    Your algorithm's time complexity must be

#Leetcode# 692. Top K Frequent Words

https://leetcode.com/problems/top-k-frequent-words/ Given a non-empty list of words, return the k most 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 l

[LC] 347. Top K Frequent Elements

Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [1,1,1,2,2,3], k = 2 Output: [1,2] Example 2: Input: nums = [1], k = 1 Output: [1] class Solution { public List<Integer> topKFrequent(int[] nums, int

[LeetCode]Top K Frequent Elements

题目描述: Given a non-empty array of integers, return the k most frequent elements. For example, Given [1,1,1,2,2,3] and k = 2, return [1,2]. Note: You may assume k is always valid, 1 ≤ k ≤ number of unique elements. Your algorithm's time complexity must

347. Top K Frequent Elements [medium] (Python)

题目链接 https://leetcode.com/problems/top-k-frequent-elements/ 题目原文 Given a non-empty array of integers, return the k most frequent elements. For example, Given [1,1,1,2,2,3] and k = 2, return [1,2]. Note: You may assume k is always valid, 1 ≤ k ≤ numbe

[LeetCode] 347. Top K Frequent Elements 解题思路 - Java

Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2,2,3] and k = 2, return [1,2]. Note: You may assume k is always valid, 1 ≤ k ≤ number of unique elements. Your algorithm's time complexity must be bet