Skip to content

Commit 988788c

Browse files
add 1471
1 parent bd7b9bb commit 988788c

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1471|[The k Strongest Values in an Array](https://leetcode.com/problems/the-k-strongest-values-in-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1471.java) | |Medium|Array, Sort|
1112
|1470|[Shuffle the Array](https://leetcode.com/problems/shuffle-the-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1470.java) | |Easy|Array|
1213
|1466|[Reorder Routes to Make All Paths Lead to the City Zero](https://leetcode.com/problems/reorder-routes-to-make-all-paths-lead-to-the-city-zero/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1466.java) | |Medium|Tree, DFS|
1314
|1464|[Maximum Product of Two Elements in an Array](https://leetcode.com/problems/maximum-product-of-two-elements-in-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1464.java) | |Easy|Array|
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Collections;
6+
import java.util.List;
7+
import java.util.TreeMap;
8+
9+
public class _1471 {
10+
public static class Solution1 {
11+
public int[] getStrongest(int[] arr, int k) {
12+
Arrays.sort(arr);
13+
int median = arr.length % 2 != 0 ? arr[arr.length / 2] : arr[arr.length / 2 - 1];
14+
TreeMap<Integer, List<Integer>> treeMap = new TreeMap<>(Collections.reverseOrder());
15+
for (int num : arr) {
16+
int diff = Math.abs(num - median);
17+
if (!treeMap.containsKey(diff)) {
18+
treeMap.put(diff, new ArrayList<>());
19+
}
20+
treeMap.get(diff).add(num);
21+
}
22+
List<Integer> sorted = new ArrayList<>();
23+
for (int key : treeMap.keySet()) {
24+
List<Integer> sort = treeMap.get(key);
25+
Collections.sort(sort, Collections.reverseOrder());
26+
sorted.addAll(sort);
27+
}
28+
int[] result = new int[k];
29+
for (int i = 0; i < k; i++) {
30+
result[i] = sorted.get(i);
31+
}
32+
return result;
33+
}
34+
}
35+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1471;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertArrayEquals;
8+
9+
public class _1471Test {
10+
private static _1471.Solution1 solution1;
11+
private static int[] nums;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _1471.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
nums = new int[]{1, 2, 3, 4, 5};
21+
assertArrayEquals(new int[]{5, 1}, solution1.getStrongest(nums, 2));
22+
}
23+
24+
}

0 commit comments

Comments
 (0)