Leetcode
2020.04.15 14:08

692. Top K Frequent Words

조회 수 93 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

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

단축키

Prev이전 문서

Next다음 문서

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

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/


?

List of Articles
번호 분류 제목 글쓴이 날짜 조회 수
17 Leetcode 287. Find the Duplicate Number hooni 2020.04.16 126
16 Leetcode 253. Meeting Rooms II hooni 2020.04.15 104
15 Leetcode 731. My Calendar II hooni 2020.04.15 100
14 Leetcode 729. My Calendar I hooni 2020.04.15 104
» Leetcode 692. Top K Frequent Words hooni 2020.04.15 93
12 Leetcode 973. K Closest Points to Origin hooni 2020.04.15 107
11 Leetcode 75. Sort Colors hooni 2020.04.14 101
10 Leetcode 2. Add Two Numbers hooni 2020.04.14 96
9 Leetcode 23. Merge k Sorted Lists hooni 2020.04.14 104
8 Leetcode 347. Top K Frequent Elements hooni 2020.04.14 102
7 Leetcode 994. Rotting Oranges file hooni 2020.04.14 115
6 Leetcode 3. Longest Substring Without Repeating Characters hooni 2020.04.09 113
5 Leetcode 62. Unique Paths file hooni 2020.04.09 105
4 Leetcode 946. Validate Stack Sequences hooni 2020.04.08 107
3 Leetcode 114. Flatten Binary Tree to Linked List hooni 2020.04.06 117
2 Leetcode 430. Flatten a Multilevel Doubly Linked List file hooni 2020.04.06 104
1 Leetcode 1055. Shortest Way to Form String hooni 2020.04.06 125
Board Pagination Prev 1 2 3 4 Next
/ 4