Skip to content

Commit 141b0c4

Browse files
add 1772
1 parent 92ad82f commit 141b0c4

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ _If you like this project, please leave me a star._ ★
1010
|-----|----------------|---------------|--------|-------------|-------------
1111
|1774|[Closest Dessert Cost](https://leetcode.com/problems/closest-dessert-cost/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1774.java) ||Medium|Greedy|
1212
|1773|[Count Items Matching a Rule](https://leetcode.com/problems/count-items-matching-a-rule/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1773.java) ||Easy|Array, String|
13+
|1772|[Sort Features by Popularity](https://leetcode.com/problems/sort-features-by-popularity/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1772.java) ||Medium|HashTable, Sort|
1314
|1769|[Minimum Number of Operations to Move All Balls to Each Box](https://leetcode.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1769.java) ||Medium|Array, Greedy|
1415
|1768|[Merge Strings Alternately](https://leetcode.com/problems/merge-strings-alternately/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1768.java) ||Easy|String|
1516
|1765|[Map of Highest Peak](https://leetcode.com/problems/map-of-highest-peak/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1765.java) ||Medium|BFS, Graph|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Arrays;
4+
import java.util.HashMap;
5+
import java.util.HashSet;
6+
import java.util.Map;
7+
import java.util.PriorityQueue;
8+
import java.util.Set;
9+
10+
public class _1772 {
11+
public static class Solution1 {
12+
public String[] sortFeatures(String[] features, String[] responses) {
13+
Map<String, Integer> map = new HashMap<>();
14+
for (int i = 0; i < features.length; i++) {
15+
map.put(features[i], i);
16+
}
17+
Map<String, Integer> countMap = new HashMap<>();
18+
for (String response : responses) {
19+
Set<String> strs = new HashSet(Arrays.asList(response.split(" ")));
20+
for (String str : strs) {
21+
if (map.containsKey(str)) {
22+
countMap.put(str, countMap.getOrDefault(str, 0) + 1);
23+
}
24+
}
25+
}
26+
PriorityQueue<Node> maxHeap = new PriorityQueue<>((a, b) -> a.freq != b.freq ? b.freq - a.freq : a.index - b.index);
27+
for (String key : map.keySet()) {
28+
maxHeap.offer(new Node(key, countMap.getOrDefault(key, 0), map.get(key)));
29+
}
30+
String[] result = new String[features.length];
31+
int i = 0;
32+
while (!maxHeap.isEmpty()) {
33+
result[i++] = maxHeap.poll().word;
34+
}
35+
return result;
36+
}
37+
38+
class Node {
39+
String word;
40+
Integer freq;
41+
Integer index;
42+
43+
public Node(String word, Integer freq, Integer index) {
44+
this.word = word;
45+
this.freq = freq;
46+
this.index = index;
47+
}
48+
}
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1772;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertArrayEquals;
8+
9+
public class _1772Test {
10+
private static _1772.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1772.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertArrayEquals(new String[]{"touch", "cooler", "lock"},
20+
solution1.sortFeatures(new String[]{"cooler", "lock", "touch"}, new String[]{"i like cooler cooler", "lock touch cool", "locker like touch"}));
21+
}
22+
23+
@Test
24+
public void test2() {
25+
26+
assertArrayEquals(new String[]{"a", "aa", "b", "c"},
27+
solution1.sortFeatures(new String[]{"a", "aa", "b", "c"}, new String[]{"a", "a aa", "a a a a a", "b a"}));
28+
}
29+
30+
}

0 commit comments

Comments
 (0)