Advertisement
1988coder

692. Top K Frequent Words

Jan 19th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.81 KB | None | 0 0
  1.  
  2. // LeetCode URL: https://leetcode.com/problems/top-k-frequent-words/
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.LinkedList;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.PriorityQueue;
  9. import java.util.TreeSet;
  10.  
  11. /**
  12.  * Find the frequency of each word in words array and use Priority Queue to find
  13.  * top k words.
  14.  *
  15.  * Time Complexity: O(N + N' log K)
  16.  *
  17.  * Space Complexity: O(N' + K). K is always less than or equal to N'. Thus the
  18.  * final space complexity: O(N')
  19.  *
  20.  * N = Length of input array. K = input k. N' = Number of unique words.
  21.  */
  22. class Solution1 {
  23.     public List<String> topKFrequent(String[] words, int k) {
  24.         LinkedList<String> result = new LinkedList<>();
  25.         if (words == null || words.length == 0 || k <= 0) {
  26.             return result;
  27.         }
  28.  
  29.         HashMap<String, Integer> map = new HashMap<>();
  30.         for (String w : words) {
  31.             map.put(w, map.getOrDefault(w, 0) + 1);
  32.         }
  33.  
  34.         PriorityQueue<Map.Entry<String, Integer>> queue = new PriorityQueue<>(k + 1,
  35.                 (a, b) -> (a.getValue().equals(b.getValue()) ? b.getKey().compareTo(a.getKey())
  36.                         : a.getValue() - b.getValue()));
  37.  
  38.         for (Map.Entry<String, Integer> e : map.entrySet()) {
  39.             queue.offer(e);
  40.             if (queue.size() > k) {
  41.                 queue.poll();
  42.             }
  43.         }
  44.  
  45.         while (!queue.isEmpty()) {
  46.             result.addFirst(queue.poll().getKey());
  47.         }
  48.         return result;
  49.     }
  50. }
  51.  
  52. /**
  53.  * Using Bucket Sort
  54.  *
  55.  * Time Complexity: O(N + N' log N')
  56.  *
  57.  * Space Complexity: O(N')
  58.  *
  59.  * N = Length of input array. N' = Number of unique words.
  60.  */
  61. class Solution2 {
  62.     public List<String> topKFrequent(String[] words, int k) {
  63.         ArrayList<String> result = new ArrayList<>();
  64.         if (words == null || words.length == 0 || k <= 0) {
  65.             return result;
  66.         }
  67.  
  68.         HashMap<String, Integer> map = new HashMap<>();
  69.         for (String w : words) {
  70.             map.put(w, map.getOrDefault(w, 0) + 1);
  71.         }
  72.  
  73.         HashMap<Integer, TreeSet<String>> buckets = new HashMap<>();
  74.  
  75.         for (String w : map.keySet()) {
  76.             int freq = map.get(w);
  77.             if (!buckets.containsKey(freq)) {
  78.                 buckets.put(freq, new TreeSet<>());
  79.             }
  80.             buckets.get(freq).add(w);
  81.         }
  82.  
  83.         int cnt = 0;
  84.  
  85.         for (int i = words.length; i > 0 && cnt < k; i--) {
  86.             if (!buckets.containsKey(i)) {
  87.                 continue;
  88.             }
  89.             for (String w : buckets.get(i)) {
  90.                 result.add(w);
  91.                 cnt++;
  92.                 if (cnt == k) {
  93.                     break;
  94.                 }
  95.             }
  96.         }
  97.  
  98.         return result;
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement