-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprob16.java
52 lines (44 loc) · 1.26 KB
/
prob16.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
51
52
import java.util.*;
import java.util.stream.Collectors;
public class prob16 {
public static List<Integer> findKClosestElements(int[] nums, int k, int target) {
int left = 0;
int right = nums.length - 1;
while (right - left >= k) {
if (Math.abs(nums[left] - target) > Math.abs(nums[right] - target)) {
left++;
} else {
right--;
}
}
int arr[] = Arrays.copyOfRange(nums, left, right + 1);
for (int i : arr) {
System.out.println(i);
}
return Arrays.stream(nums, left, right + 1).boxed()
.collect(Collectors.toList());
}
public static void findKClosestElements2(int[] nums, int k, int target) {
HashMap<Integer, Integer> hm = new HashMap<>();
for (int num : nums) {
if (!hm.containsKey(num)) {
hm.put(num, Math.abs(target - num));
}
}
PriorityQueue<Integer> q = new PriorityQueue<>((n1, n2) -> hm.get(n2) - hm.get(n1));
for (int key : hm.keySet()) {
q.add(key);
if (q.size() > k) {
q.poll();
}
}
for (int val : q) {
System.out.println(val);
}
}
public static void main(String[] args) {
int[] nums = { 10, 12, 15, 17, 18, 20, 25 };
int target = 16, k = 4;
findKClosestElements2(nums, k, target);
}
}