forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_1772.java
50 lines (46 loc) · 1.68 KB
/
_1772.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.fishercoder.solutions;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Set;
public class _1772 {
public static class Solution1 {
public String[] sortFeatures(String[] features, String[] responses) {
Map<String, Integer> map = new HashMap<>();
for (int i = 0; i < features.length; i++) {
map.put(features[i], i);
}
Map<String, Integer> countMap = new HashMap<>();
for (String response : responses) {
Set<String> strs = new HashSet(Arrays.asList(response.split(" ")));
for (String str : strs) {
if (map.containsKey(str)) {
countMap.put(str, countMap.getOrDefault(str, 0) + 1);
}
}
}
PriorityQueue<Node> maxHeap = new PriorityQueue<>((a, b) -> a.freq != b.freq ? b.freq - a.freq : a.index - b.index);
for (String key : map.keySet()) {
maxHeap.offer(new Node(key, countMap.getOrDefault(key, 0), map.get(key)));
}
String[] result = new String[features.length];
int i = 0;
while (!maxHeap.isEmpty()) {
result[i++] = maxHeap.poll().word;
}
return result;
}
class Node {
String word;
Integer freq;
Integer index;
public Node(String word, Integer freq, Integer index) {
this.word = word;
this.freq = freq;
this.index = index;
}
}
}
}