Leetcode

692. Top K Frequent Words

by hooni posted Apr 15, 2020
?

단축키

Prev이전 문서

Next다음 문서

ESC닫기

크게 작게 위로 아래로 댓글로 가기 인쇄

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:

  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(n log k) time and O(n) extra space.


class Solution {
    public List<String> topKFrequent(String[] words, int k) {
        HashMap<String, Integer> hmap = new HashMap<>();
        /*
        for(int i=0; i<words.length; i++){
            if(hmap.containsKey(words[i])){
                hmap.put(words[i], hmap.get(words[i])+1);
            }else{
                hmap.put(words[i], 1);
            }
        }
        */
        for(String word : words){
            hmap.put(word, hmap.getOrDefault(word, 0) + 1);
        }
 
        //PriorityQueue<Map.Entry<String, Integer>> queue = new PriorityQueue<>(Comparator.comparing(e -> e.getValue()));
        PriorityQueue<Map.Entry<String, Integer>> queue = new PriorityQueue<>(new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> item1, Map.Entry<String, Integer> item2){
                if(item1.getValue() == item2.getValue()){
                    return item1.getKey().compareTo(item2.getKey());
                }else{
                    return item2.getValue() - item1.getValue();
                }
            }
        });
             
        for(Map.Entry<String, Integer> item : hmap.entrySet()){
            queue.offer(item);
        }
         
        List<String> list = new ArrayList<>();
        for(int i=0; i<k; i++){
            list.add(queue.poll().getKey());
        }
         
        return list;
    }
}

class Solution {
    public List<String> topKFrequent(String[] words, int k) {
        HashMap<String, Integer> hmap = new HashMap<>();
        /* //before
        for(int i=0; i<words.length; i++){
            if(hmap.containsKey(words[i])){
                hmap.put(words[i], hmap.get(words[i])+1);
            }else{
                hmap.put(words[i], 1);
            }
        }
        PriorityQueue<String> queue = new PriorityQueue<>(
            (s1, s2) -> {
                if(hmap.get(s1) == hmap.get(s2)){
                    return s2.compareTo(s1);
                }else{
                    return hmap.get(s1) - hmap.get(s2);
                }
            }
        );
        */

        for(String word : words){
            hmap.put(word, hmap.getOrDefault(word, 0) + 1);
        }

        PriorityQueue<String> queue = new PriorityQueue<>(
            (s1, s2) -> hmap.get(s1) == hmap.get(s2) ? s2.compareTo(s1) : hmap.get(s1) - hmap.get(s2)
        );
         
        for (String word: hmap.keySet()) {
            queue.offer(word);
            if (queue.size() > k){
                queue.poll();
            }
        }
         
        List<String> list = new ArrayList<>();
        for(int i=0; i<k; i++){
            list.add(queue.poll());
        }
         
        Collections.reverse(list);
         
        return list;
    }
}

















class Solution {
    public List<String> topKFrequent(String[] words, int k) {
        HashMap<String, Integer> hmap = new HashMap();
  
        /* //before
        for(int i=0; i<words.length; i++){
            if(hmap.containsKey(words[i])){
                hmap.put(words[i], hmap.get(words[i])+1);
            }else{
                hmap.put(words[i], 1);
            }
        }
        List<String> list = new ArrayList(hmap.keySet());
        Collections.sort(
            list, (s1, s2) -> {
                if(hmap.get(s1) == hmap.get(s2)){
                    return s1.compareTo(s2);
                }else{
                    return hmap.get(s2) - hmap.get(s1);
                }
            }
        );
        */
        
        for (String word: words) {
            hmap.put(word, hmap.getOrDefault(word, 0) + 1);
        }
        
        Collections.sort(
            list, (s1, s2) -> hmap.get(s1) == hmap.get(s2) ? s1.compareTo(s2) : hmap.get(s2) - hmap.get(s1)
        );
        
        return list.subList(0, k);
    }
}


[문제] https://leetcode.com/problems/top-k-frequent-words/