-
Notifications
You must be signed in to change notification settings - Fork 277
/
FindKClosestElements.java
49 lines (43 loc) · 1.34 KB
/
FindKClosestElements.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
class Solution {
// TC : O(n) Linear Search
public List<Integer> findClosestElements(int[] arr, int k, int x) {
List<Integer> ans = new ArrayList<>();
int low = 0;
int high = arr.length-1;
while(high - low >=k){
if(Math.abs(arr[low] - x) > Math.abs(arr[high] - x) ){
low++;
} else {
high--;
}
}
for(int i=low;i<=high;i++){
ans.add(arr[i]);
}
return ans;
}
// Solution 2 : PriorityQueue
private class Node {
private int index;
private int diff;
Node(int index, int diff){
this.index= index;
this.diff = diff;
}
}
public List<Integer> findClosestElements(int[] arr, int k, int x) {
PriorityQueue<Node> pq = new PriorityQueue<>((a,b) ->((b.diff == a.diff) ? (b.index - a.index) : (b.diff - a.diff)));
for(int i= 0;i<arr.length;i++){
pq.offer(new Node(i, Math.abs(arr[i] - x)));
if(pq.size()>k){
pq.poll();
}
}
List<Integer> ans = new ArrayList<>();
while(!pq.isEmpty()) {
ans.add(arr[pq.poll().index]);
}
Collections.sort(ans);
return ans;
}
}