forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
_658.java
74 lines (68 loc) · 2.18 KB
/
_658.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.List;
/**
* 658. Find K Closest Elements
*
* Given a sorted array, two integers k and x,
* find the k closest elements to x in the array.
* The result should also be sorted in ascending order.
* If there is a tie, the smaller elements are always preferred.
Example 1:
Input: [1,2,3,4,5], k=4, x=3
Output: [1,2,3,4]
Example 2:
Input: [1,2,3,4,5], k=4, x=-1
Output: [1,2,3,4]
Note:
The value k is positive and will always be smaller than the length of the sorted array.
Length of the given array is positive and will not exceed 104
Absolute value of elements in the array and x will not exceed 104
*/
public class _658 {
public List<Integer> findClosestElements(List<Integer> arr, int k, int x) {
List<Integer> result = new ArrayList<>();
int index = findInsertPosition(arr, x);
int right = 0;
if (arr.get(index) == x) {
result.add(arr.get(index));
right = index + 1;
k--;
} else {
right = index;
}
int left = index - 1;
while (k-- > 0) {
if (left >= 0 && right < arr.size()) {
if (x - arr.get(left) <= arr.get(right) - x) {
result.add(0, arr.get(left--));
} else {
result.add(result.size(), arr.get(right++));
}
} else if (left >= 0) {
result.add(0, arr.get(left--));
} else if (right < arr.size()) {
result.add(result.size(), arr.get(right++));
}
}
return result;
}
private int findInsertPosition(List<Integer> arr, int x) {
if (arr == null || arr.size() == 0) {
return 0;
}
int len = arr.size();
for (int i = 0; i < len; i++) {
if (arr.get(0) > x) {
return 0;
} else if (arr.get(len - 1) < x) {
return len;
} else if (arr.get(i) < x && arr.get(i + 1) > x) {
return i + 1;
} else if (arr.get(i) == x) {
return i;
}
}
return len;
}
}