|
| 1 | +// Just Sorting |
| 2 | +// The easiest way to think of this problem and easy to implement. |
| 3 | +// Time complexity: O(nlogn), naive sort is o(nlogn) |
| 4 | +// Space complexity: O(n), for map and list |
| 5 | + |
| 6 | +class Solution { |
| 7 | + public List<String> topKFrequent(String[] words, int k) { |
| 8 | + Map<String, Integer> map = new HashMap<>(); |
| 9 | + for(String word:words){ |
| 10 | + map.put(word, map.getOrDefault(word, 0)+1); |
| 11 | + } |
| 12 | + List<Map.Entry<String, Integer>> l = new LinkedList<>(); |
| 13 | + for(Map.Entry<String, Integer> e:map.entrySet()){ |
| 14 | + l.add(e); |
| 15 | + } |
| 16 | + Collections.sort(l, new MyComparator());//just use our Comparator to sort |
| 17 | + List<String> ans = new LinkedList<>(); |
| 18 | + for(int i = 0;i<=k-1;i++){ |
| 19 | + ans.add(l.get(i).getKey()); |
| 20 | + } |
| 21 | + return ans; |
| 22 | + } |
| 23 | +} |
| 24 | +/* |
| 25 | +// Implement our own comparator for this problem, I will also use this Comparator in other methods(A little different in minHeap method). |
| 26 | +// We can also use anonymous Comparaotr or Lambda function. |
| 27 | +// */ |
| 28 | +class MyComparator implements Comparator<Map.Entry<String, Integer>> { |
| 29 | + |
| 30 | + public int compare(Map.Entry<String, Integer> e1, Map.Entry<String, Integer> e2){ |
| 31 | + String word1 = e1.getKey(); |
| 32 | + int freq1 = e1.getValue(); |
| 33 | + String word2 = e2.getKey(); |
| 34 | + int freq2 = e2.getValue(); |
| 35 | + if(freq1!=freq2){ |
| 36 | + return freq2-freq1; |
| 37 | + } |
| 38 | + else { |
| 39 | + return word1.compareTo(word2); |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | +Max Heap |
| 44 | +Maintain a max heap and add all the words in it. Pop top K words to get the results. |
| 45 | +Time Complexity: O(nlogn + Klogn) = O(nlogn) |
| 46 | +Space Complexity: O(n), for heap |
| 47 | + |
| 48 | +class Solution { |
| 49 | + public List<String> topKFrequent(String[] words, int k) { |
| 50 | + Map<String, Integer> map = new HashMap<>(); |
| 51 | + for(String word:words){ |
| 52 | + map.put(word, map.getOrDefault(word, 0)+1); |
| 53 | + } |
| 54 | + PriorityQueue<Map.Entry<String, Integer>> pq = new PriorityQueue<>(new MyComparator()); |
| 55 | + for(Map.Entry<String, Integer> e:map.entrySet()){ |
| 56 | + pq.offer(e); |
| 57 | + } |
| 58 | + List<String> ans = new LinkedList<>(); |
| 59 | + for(int i = 0;i<=k-1;i++){ |
| 60 | + ans.add(pq.poll().getKey()); |
| 61 | + } |
| 62 | + return ans; |
| 63 | + } |
| 64 | +} |
| 65 | +class MyComparator implements Comparator<Map.Entry<String, Integer>> { |
| 66 | + public int compare(Map.Entry<String, Integer> e1, Map.Entry<String, Integer> e2){ |
| 67 | + String word1 = e1.getKey(); |
| 68 | + int freq1 = e1.getValue(); |
| 69 | + String word2 = e2.getKey(); |
| 70 | + int freq2 = e2.getValue(); |
| 71 | + if(freq1!=freq2){ |
| 72 | + return freq2-freq1; |
| 73 | + } |
| 74 | + else { |
| 75 | + return word1.compareTo(word2); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | +// Min Heap |
| 80 | +// Instead of using a max heap, we only store Top K Freqency word we have met so far in our min heap. |
| 81 | +// Time Complexity: O(nlogK), logK time for each word |
| 82 | +// Space Complexity: O(K), since the largest number of words in our minheap is K |
| 83 | + |
| 84 | +class Solution { |
| 85 | + public List<String> topKFrequent(String[] words, int k) { |
| 86 | + Map<String, Integer> map = new HashMap<>(); |
| 87 | + for(String word:words){ |
| 88 | + map.put(word, map.getOrDefault(word, 0)+1); |
| 89 | + } |
| 90 | + MyComparator comparator = new MyComparator(); |
| 91 | + PriorityQueue<Map.Entry<String, Integer>> pq = new PriorityQueue<>(comparator); |
| 92 | + for(Map.Entry<String, Integer> e:map.entrySet()){ |
| 93 | + // If minHeap's size is smaller than K, we just add the entry |
| 94 | + if(pq.size()<k){ |
| 95 | + pq.offer(e); |
| 96 | + } |
| 97 | + // Else, we compare the current entry with "min" entry in priority queue |
| 98 | + else { |
| 99 | + if(comparator.compare(e, pq.peek())>0){ |
| 100 | + pq.poll(); |
| 101 | + pq.offer(e); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + List<String> ans = new LinkedList<>(); |
| 106 | + for(int i = 0;i<=k-1;i++){ |
| 107 | + ans.add(0, pq.poll().getKey());//the "smaller" entry poll out ealier |
| 108 | + } |
| 109 | + return ans; |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +// The comparaotr is reversed as maxHeap |
| 114 | +class MyComparator implements Comparator<Map.Entry<String, Integer>> { |
| 115 | + public int compare(Map.Entry<String, Integer> e1, Map.Entry<String, Integer> e2){ |
| 116 | + String word1 = e1.getKey(); |
| 117 | + int freq1 = e1.getValue(); |
| 118 | + String word2 = e2.getKey(); |
| 119 | + int freq2 = e2.getValue(); |
| 120 | + if(freq1!=freq2){ |
| 121 | + return freq1-freq2; |
| 122 | + } |
| 123 | + else { |
| 124 | + return word2.compareTo(word1); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | +// Bucket sort + Trie |
| 129 | +// This method is derived from 347. Top K Frequent Elements. At 347, we use bucket sort(LinkedList in each bucket) to find top K frequency integers and we can choose any integer if there is a tie of frequency . But in this question, the problem is that when there is a tie of frequency, we need to compare the lexicographic order. Thus using bucket sort(LinkedList in each bucket) is not good. |
| 130 | +// The way to solve the tie problem is to use either trie or BST. |
| 131 | +// Time Complexity: O(nm) = O(n), m time to construct trie for each word and m is a constant |
| 132 | +// Space Complexity: O(nm) = O(n), m space for each bucket and m is a constant |
| 133 | + |
| 134 | +class Solution { |
| 135 | + public List<String> topKFrequent(String[] words, int k) { |
| 136 | + Map<String, Integer> map = new HashMap<>(); |
| 137 | + for(String word:words){ |
| 138 | + map.put(word, map.getOrDefault(word, 0)+1); |
| 139 | + } |
| 140 | + |
| 141 | + Trie[] buckets = new Trie[words.length]; |
| 142 | + for(Map.Entry<String, Integer> e:map.entrySet()){ |
| 143 | + //for each word, add it into trie at its bucket |
| 144 | + String word = e.getKey(); |
| 145 | + int freq = e.getValue(); |
| 146 | + if(buckets[freq]==null){ |
| 147 | + buckets[freq] = new Trie(); |
| 148 | + } |
| 149 | + buckets[freq].addWord(word); |
| 150 | + } |
| 151 | + |
| 152 | + List<String> ans = new LinkedList<>(); |
| 153 | + |
| 154 | + for(int i = buckets.length-1;i>=0;i--){ |
| 155 | + //for trie in each bucket, get all the words with same frequency in lexicographic order. Compare with k and get the result |
| 156 | + if(buckets[i]!=null){ |
| 157 | + List<String> l = new LinkedList<>(); |
| 158 | + buckets[i].getWords(buckets[i].root, l); |
| 159 | + if(l.size()<k){ |
| 160 | + ans.addAll(l); |
| 161 | + k = k - l.size(); |
| 162 | + } |
| 163 | + else { |
| 164 | + for(int j = 0;j<=k-1;j++){ |
| 165 | + ans.add(l.get(j)); |
| 166 | + } |
| 167 | + break; |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + return ans; |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +class TrieNode { |
| 176 | + TrieNode[] children = new TrieNode[26]; |
| 177 | + String word = null; |
| 178 | +} |
| 179 | + |
| 180 | +class Trie { |
| 181 | + TrieNode root = new TrieNode(); |
| 182 | + public void addWord(String word){ |
| 183 | + TrieNode cur = root; |
| 184 | + for(char c:word.toCharArray()){ |
| 185 | + if(cur.children[c-'a']==null){ |
| 186 | + cur.children[c-'a'] = new TrieNode(); |
| 187 | + } |
| 188 | + cur = cur.children[c-'a']; |
| 189 | + } |
| 190 | + cur.word = word; |
| 191 | + } |
| 192 | + |
| 193 | + public void getWords(TrieNode node, List<String> ans){ |
| 194 | + //use DFS to get lexicograpic order of all the words with same frequency |
| 195 | + if(node==null){ |
| 196 | + return; |
| 197 | + } |
| 198 | + if(node.word!=null){ |
| 199 | + ans.add(node.word); |
| 200 | + } |
| 201 | + for(int i = 0;i<=25;i++){ |
| 202 | + if(node.children[i]!=null){ |
| 203 | + getWords(node.children[i], ans); |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + } |
| 208 | +} |
| 209 | +// Bucket sort + BST |
| 210 | +// The reason we use Trie is to break the tie of same word frequency. Thus we can easily use BST to replace Trie(In Java, we can use TreeMap or TreeSet) |
| 211 | +// Time Complexity: O(n), not sure |
| 212 | +// Space Complexity: O(n), not sure |
| 213 | + |
| 214 | +class Solution { |
| 215 | + public List<String> topKFrequent(String[] words, int k) { |
| 216 | + Map<String, Integer> map = new HashMap<>(); |
| 217 | + for(String word:words){ |
| 218 | + map.put(word, map.getOrDefault(word, 0)+1); |
| 219 | + } |
| 220 | + TreeMap<String, Integer>[] buckets = new TreeMap[words.length]; |
| 221 | + for(Map.Entry<String, Integer> e:map.entrySet()){ |
| 222 | + String word = e.getKey(); |
| 223 | + int freq = e.getValue(); |
| 224 | + if(buckets[freq]==null){ |
| 225 | + buckets[freq] = new TreeMap<>((a, b)->{ |
| 226 | + return a.compareTo(b); |
| 227 | + }); |
| 228 | + } |
| 229 | + buckets[freq].put(word, freq); |
| 230 | + } |
| 231 | + |
| 232 | + List<String> ans = new LinkedList<>(); |
| 233 | + for(int i = buckets.length-1;i>=0;i--){ |
| 234 | + if(buckets[i]!=null){ |
| 235 | + TreeMap<String, Integer> temp = buckets[i]; |
| 236 | + if(temp.size()<k){ |
| 237 | + k = k - temp.size(); |
| 238 | + while(temp.size()>0){ |
| 239 | + ans.add(temp.pollFirstEntry().getKey()); |
| 240 | + } |
| 241 | + } |
| 242 | + else { |
| 243 | + while(k>0){ |
| 244 | + ans.add(temp.pollFirstEntry().getKey()); |
| 245 | + k--; |
| 246 | + } |
| 247 | + break; |
| 248 | + } |
| 249 | + } |
| 250 | + } |
| 251 | + return ans; |
| 252 | + } |
| 253 | +} |
| 254 | +// Quick select |
| 255 | +// If the question is to find Kth frequency word, quick select is a good solution and only cost O(n), for this question, after getting Top K frequency words by using quick select, we also need to do a sort to make sure they are in the right order. |
| 256 | +// Time Complexity: O(n+KlogK), n time for quick select and KlogK time for sort |
| 257 | +// Space Complexity: O(n) |
| 258 | + |
| 259 | +class Solution { |
| 260 | + public List<String> topKFrequent(String[] words, int k) { |
| 261 | + Map<String, Integer> map = new HashMap<>(); |
| 262 | + for(String word:words){ |
| 263 | + map.put(word, map.getOrDefault(word, 0)+1); |
| 264 | + } |
| 265 | + |
| 266 | + Map.Entry<String, Integer>[] entrys = new Map.Entry[map.size()]; |
| 267 | + int index = 0; |
| 268 | + for(Map.Entry<String, Integer> e:map.entrySet()){ |
| 269 | + entrys[index] = e; |
| 270 | + index++; |
| 271 | + } |
| 272 | + //do quick select |
| 273 | + int start = 0; |
| 274 | + int end = entrys.length-1; |
| 275 | + int mid = 0; |
| 276 | + while(start<=end){ |
| 277 | + mid = partition(entrys, start, end); |
| 278 | + if(mid == k-1){ |
| 279 | + break; |
| 280 | + } |
| 281 | + else if(mid<k-1){ |
| 282 | + start = mid + 1; |
| 283 | + } |
| 284 | + else { |
| 285 | + end = mid - 1; |
| 286 | + } |
| 287 | + } |
| 288 | + |
| 289 | + List<String> ans = new LinkedList<>(); |
| 290 | + List<Map.Entry<String, Integer>> l = new LinkedList<>(); |
| 291 | + for(int i = 0;i<=mid;i++){ |
| 292 | + l.add(entrys[i]); |
| 293 | + } |
| 294 | + //still need to sort these K words, because we only know they are in result, but not in right order |
| 295 | + Collections.sort(l, new MyComparator()); |
| 296 | + for(Map.Entry<String, Integer> e:l){ |
| 297 | + ans.add(e.getKey()); |
| 298 | + } |
| 299 | + return ans; |
| 300 | + } |
| 301 | + |
| 302 | + private int partition(Map.Entry<String, Integer>[] entrys, int start, int end){ |
| 303 | + int pivot = start; |
| 304 | + int left = start + 1; |
| 305 | + int right = end; |
| 306 | + MyComparator myComparator = new MyComparator(); |
| 307 | + while(true){ |
| 308 | + while(left<=end){ |
| 309 | + if(myComparator.compare(entrys[left], entrys[pivot])<=0){ |
| 310 | + left++; |
| 311 | + } |
| 312 | + else { |
| 313 | + break; |
| 314 | + } |
| 315 | + } |
| 316 | + |
| 317 | + while(right>=start+1){ |
| 318 | + if(myComparator.compare(entrys[right], entrys[pivot])>0){ |
| 319 | + right--; |
| 320 | + } |
| 321 | + else { |
| 322 | + break; |
| 323 | + } |
| 324 | + } |
| 325 | + if(left>right){ |
| 326 | + break; |
| 327 | + } |
| 328 | + swap(entrys, left, right); |
| 329 | + } |
| 330 | + swap(entrys, pivot, right); |
| 331 | + return right; |
| 332 | + } |
| 333 | + |
| 334 | + private void swap(Map.Entry<String, Integer>[] entrys, int i, int j){ |
| 335 | + Map.Entry<String, Integer> a = entrys[i]; |
| 336 | + entrys[i] = entrys[j]; |
| 337 | + entrys[j] = a; |
| 338 | + } |
| 339 | +} |
| 340 | + |
| 341 | +class MyComparator implements Comparator<Map.Entry<String, Integer>> { |
| 342 | + |
| 343 | + public int compare(Map.Entry<String, Integer> e1, Map.Entry<String, Integer> e2){ |
| 344 | + String word1 = e1.getKey(); |
| 345 | + int freq1 = e1.getValue(); |
| 346 | + String word2 = e2.getKey(); |
| 347 | + int freq2 = e2.getValue(); |
| 348 | + if(freq1!=freq2){ |
| 349 | + return freq2-freq1; |
| 350 | + } |
| 351 | + else { |
| 352 | + return word1.compareTo(word2); |
| 353 | + } |
| 354 | + } |
| 355 | +} |
0 commit comments