Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit141b0c4

Browse files
add 1772
1 parent92ad82f commit141b0c4

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
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|
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
packagecom.fishercoder.solutions;
2+
3+
importjava.util.Arrays;
4+
importjava.util.HashMap;
5+
importjava.util.HashSet;
6+
importjava.util.Map;
7+
importjava.util.PriorityQueue;
8+
importjava.util.Set;
9+
10+
publicclass_1772 {
11+
publicstaticclassSolution1 {
12+
publicString[]sortFeatures(String[]features,String[]responses) {
13+
Map<String,Integer>map =newHashMap<>();
14+
for (inti =0;i <features.length;i++) {
15+
map.put(features[i],i);
16+
}
17+
Map<String,Integer>countMap =newHashMap<>();
18+
for (Stringresponse :responses) {
19+
Set<String>strs =newHashSet(Arrays.asList(response.split(" ")));
20+
for (Stringstr :strs) {
21+
if (map.containsKey(str)) {
22+
countMap.put(str,countMap.getOrDefault(str,0) +1);
23+
}
24+
}
25+
}
26+
PriorityQueue<Node>maxHeap =newPriorityQueue<>((a,b) ->a.freq !=b.freq ?b.freq -a.freq :a.index -b.index);
27+
for (Stringkey :map.keySet()) {
28+
maxHeap.offer(newNode(key,countMap.getOrDefault(key,0),map.get(key)));
29+
}
30+
String[]result =newString[features.length];
31+
inti =0;
32+
while (!maxHeap.isEmpty()) {
33+
result[i++] =maxHeap.poll().word;
34+
}
35+
returnresult;
36+
}
37+
38+
classNode {
39+
Stringword;
40+
Integerfreq;
41+
Integerindex;
42+
43+
publicNode(Stringword,Integerfreq,Integerindex) {
44+
this.word =word;
45+
this.freq =freq;
46+
this.index =index;
47+
}
48+
}
49+
}
50+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._1772;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticorg.junit.Assert.assertArrayEquals;
8+
9+
publicclass_1772Test {
10+
privatestatic_1772.Solution1solution1;
11+
12+
@BeforeClass
13+
publicstaticvoidsetup() {
14+
solution1 =new_1772.Solution1();
15+
}
16+
17+
@Test
18+
publicvoidtest1() {
19+
assertArrayEquals(newString[]{"touch","cooler","lock"},
20+
solution1.sortFeatures(newString[]{"cooler","lock","touch"},newString[]{"i like cooler cooler","lock touch cool","locker like touch"}));
21+
}
22+
23+
@Test
24+
publicvoidtest2() {
25+
26+
assertArrayEquals(newString[]{"a","aa","b","c"},
27+
solution1.sortFeatures(newString[]{"a","aa","b","c"},newString[]{"a","a aa","a a a a a","b a"}));
28+
}
29+
30+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp