Top K Frequent Words

Given a non-empty list of words, return thekmost 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:

  1. You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
  2. Input words contain only lowercase letters.

Follow up:

  1. Try to solve it in O (nlogk) time and O(n) extra space.

Analysis

Approach 1 - Sorting

Time Complexity: O(NlogN) where N is the length of words. We count the frequency of each word in O(N) time, then we sort the given words in O(NlogN) time.

Space Complexity: O(N), the space used to store our candidates.

Approach 1 - Heap

Min Heap - Keep a k-sized min heap while add keys from the frequency counter, poll the heap until it's empty and reverse the list

Max Heap - Add all keys from the word frequency counter to max heap, and poll k times

Time Complexity: O(Nlogk), where N is the length of words. We count the frequency of each word in O(N) time, then we add NN words to the heap, each in O(logk) time. Finally, we pop from the heap up to kk times. As k \leq Nk≤N, this is O(Nlogk) in total.

Space Complexity: O(N), the space used to store our count.

Solution

Heap - PriorityQueue (50ms 49.63%)

class Solution {
    public List<String> topKFrequent(String[] words, int k) {
        Map<String, Integer> count = new HashMap<String, Integer>();
        List<String> result = new LinkedList<String>();
        for (String word : words) {
            count.put(word, count.getOrDefault(word, 0) + 1);
        }
        Queue<String> pq = new PriorityQueue<String>((w1, w2) -> count.get(w1).equals(count.get(w2)) ? w1.compareTo(w2) : count.get(w2) - count.get(w1));
        for (String word : count.keySet()) {
            pq.offer(word);
        }
        for (int i = 0; i < k; i++) {
            result.add(pq.poll());
        }
        return result;
    }
}

Heap - PriorityQueue with Custom Class (9ms 100% AC)

class Solution {
    public List<String> topKFrequent(String[] words, int k) {
        List<String> result = new ArrayList<String>();
        Map<String,Node> map = new HashMap<>();
        for(String s: words)
        {
            if(!map.containsKey(s))
                map.put(s,new Node(s));
            else
                map.get(s).inc();
        }
        Queue<Node> q = new PriorityQueue<Node>(new Comparator<Node>(){
           @Override
            public int compare(Node a, Node b)
            {
                int result = b.count-a.count;
                if(result == 0)
                    return a.s.compareTo(b.s);
                return result;
            }
        });
        for(Map.Entry<String,Node> entry: map.entrySet())
        {
            q.offer(entry.getValue());
        }
        for(int i=0;i<k;i++)
            result.add(q.poll().s);
        return result;
    }
}
class Node
{
    String s;
    int count;
    Node(String s)
    {
        this.s = s;
        count = 1;
    }
    void inc()
    {
        count++;
    }
}

Sorting - (49ms 50.88%)

class Solution {
    public List<String> topKFrequent(String[] words, int k) {
        Map<String, Integer> count = new HashMap();
        for (String word: words) {
            count.put(word, count.getOrDefault(word, 0) + 1);
        }
        List<String> candidates = new ArrayList(count.keySet());
        Collections.sort(candidates, (w1, w2) -> count.get(w1).equals(count.get(w2)) ?
                w1.compareTo(w2) : count.get(w2) - count.get(w1));

        return candidates.subList(0, k);
}

results matching ""

    No results matching ""