forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_1471.java
35 lines (33 loc) · 1.2 KB
/
_1471.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
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.TreeMap;
public class _1471 {
public static class Solution1 {
public int[] getStrongest(int[] arr, int k) {
Arrays.sort(arr);
int median = arr.length % 2 != 0 ? arr[arr.length / 2] : arr[arr.length / 2 - 1];
TreeMap<Integer, List<Integer>> treeMap = new TreeMap<>(Collections.reverseOrder());
for (int num : arr) {
int diff = Math.abs(num - median);
if (!treeMap.containsKey(diff)) {
treeMap.put(diff, new ArrayList<>());
}
treeMap.get(diff).add(num);
}
List<Integer> sorted = new ArrayList<>();
for (int key : treeMap.keySet()) {
List<Integer> sort = treeMap.get(key);
Collections.sort(sort, Collections.reverseOrder());
sorted.addAll(sort);
}
int[] result = new int[k];
for (int i = 0; i < k; i++) {
result[i] = sorted.get(i);
}
return result;
}
}
}