Skip to content

Commit

Permalink
feat: Top K Frequent Words
Browse files Browse the repository at this point in the history
  • Loading branch information
hongbo-miao committed Nov 11, 2019
1 parent 1d63e32 commit 7ba6b4d
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
49 changes: 49 additions & 0 deletions JavaScript/0692. Top K Frequent Words.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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 lower alphabetical order comes first.
//
// Example 1:
// Input: ["i", "love", "leetcode", "i", "love", "coding"], k = 2
// Output: ["i", "love"]
// Explanation: "i" and "love" are the two most frequent words.
// Note that "i" comes before "love" due to a lower alphabetical order.
//
// Example 2:
// Input: ["the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"], k = 4
// Output: ["the", "is", "sunny", "day"]
// Explanation: "the", "is", "sunny" and "day" are the four most frequent words,
// with the number of occurrence being 4, 3, 2 and 1 respectively.
//
// 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(n log k) time and O(n) extra space.

/**
* @param {string[]} words
* @param {number} k
* @return {string[]}
*/

/** 1) Sorting */
// Time O(n log n)
// Space O(n)
const topKFrequent = (words, k) => {
if (words == null || words.length === 0) return null;

const map = {};
for (const w of words) {
if (map[w] == null) map[w] = 0;
map[w]++;
}

const compare = (w1, w2) => {
if (map[w1] !== map[w2]) return map[w2] - map[w1];
return w1.localeCompare(w2);
};
return Object.keys(map).sort(compare).slice(0, k);
};

/** 2) Priority Queue */
// JavaScript is lack of priority queue, check Python version
39 changes: 39 additions & 0 deletions Python/0692. Top K Frequent Words.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# 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 lower alphabetical order comes first.
#
# Example 1:
# Input: ["i", "love", "leetcode", "i", "love", "coding"], k = 2
# Output: ["i", "love"]
# Explanation: "i" and "love" are the two most frequent words.
# Note that "i" comes before "love" due to a lower alphabetical order.
#
# Example 2:
# Input: ["the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"], k = 4
# Output: ["the", "is", "sunny", "day"]
# Explanation: "the", "is", "sunny" and "day" are the four most frequent words,
# with the number of occurrence being 4, 3, 2 and 1 respectively.
#
# 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(n log k) time and O(n) extra space.

"""
Priority Queue
"""
# https://leetcode.com/problems/top-k-frequent-words/solution/
#
# Time O(N + klogN): heapq.heapify operation and counting operations are O(N), and each of k heapq.heappop operations are O(logN).
# Space O(N), the space used to store our count.
import heapq
from collections import Counter


class Solution:
def topKFrequent(self, words: List[str], k: int) -> List[str]:
count = Counter(words)
q = [(-freq, word) for word, freq in count.items()]
heapq.heapify(q)
return [heapq.heappop(q)[1] for _ in range(k)]

0 comments on commit 7ba6b4d

Please sign in to comment.