题目描述
给定一个 排序好 的数组 arr
,两个整数 k
和 x
,从数组中找到最靠近 x
(两数之差最小)的 k
个数。返回的结果必须要是按升序排好的。
整数 a
比整数 b
更接近 x
需要满足:
|a - x| < |b - x|
或者
|a - x| == |b - x|
且 a < b
示例 1:
输入:arr = [1,2,3,4,5], k = 4, x = 3
输出:[1,2,3,4]
示例 2:
输入:arr = [1,1,2,3,4,5], k = 4, x = -1
输出:[1,1,2,3]
提示:
1 <= k <= arr.length
1 <= arr.length <= 104
arr
按 升序 排列
-104 <= arr[i], x <= 104
解法
方法一:排序
将 $arr$ 中的所有元素按照与 $x$ 的距离从小到大进行排列。取前 $k$ 个元素排序后返回。
时间复杂度 $O(nlogn)$。
| class Solution:
def findClosestElements(self, arr: List[int], k: int, x: int) -> List[int]:
arr.sort(key=lambda v: abs(v - x))
return sorted(arr[:k])
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14 | class Solution {
public List<Integer> findClosestElements(int[] arr, int k, int x) {
List<Integer> ans = Arrays.stream(arr)
.boxed()
.sorted((a, b) -> {
int v = Math.abs(a - x) - Math.abs(b - x);
return v == 0 ? a - b : v;
})
.collect(Collectors.toList());
ans = ans.subList(0, k);
Collections.sort(ans);
return ans;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 | int target;
class Solution {
public:
static bool cmp(int& a, int& b) {
int v = abs(a - target) - abs(b - target);
return v == 0 ? a < b : v < 0;
}
vector<int> findClosestElements(vector<int>& arr, int k, int x) {
target = x;
sort(arr.begin(), arr.end(), cmp);
vector<int> ans(arr.begin(), arr.begin() + k);
sort(ans.begin(), ans.end());
return ans;
}
};
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 | func findClosestElements(arr []int, k int, x int) []int {
sort.Slice(arr, func(i, j int) bool {
v := abs(arr[i]-x) - abs(arr[j]-x)
if v == 0 {
return arr[i] < arr[j]
}
return v < 0
})
ans := arr[:k]
sort.Ints(ans)
return ans
}
func abs(x int) int {
if x >= 0 {
return x
}
return -x
}
|
1
2
3
4
5
6
7
8
9
10
11
12 | function findClosestElements(arr: number[], k: number, x: number): number[] {
let l = 0;
let r = arr.length;
while (r - l > k) {
if (x - arr[l] <= arr[r - 1] - x) {
--r;
} else {
++l;
}
}
return arr.slice(l, r);
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 | impl Solution {
pub fn find_closest_elements(arr: Vec<i32>, k: i32, x: i32) -> Vec<i32> {
let n = arr.len();
let mut l = 0;
let mut r = n;
while r - l != (k as usize) {
if x - arr[l] <= arr[r - 1] - x {
r -= 1;
} else {
l += 1;
}
}
arr[l..r].to_vec()
}
}
|
方法二:双指针
直觉上,有序数组 $arr$ 最靠近 $x$ 的 $k$ 个数必然是一段连续的子数组。
我们可以声明头尾指针,记为 $l$ 和 $r$,然后根据 $x-arr[l]$ 与 $arr[r-1] - x$ 的大小比较结果缩小范围,直到 $r - l = k$。
时间复杂度 $O(n)$。
| class Solution:
def findClosestElements(self, arr: List[int], k: int, x: int) -> List[int]:
l, r = 0, len(arr)
while r - l > k:
if x - arr[l] <= arr[r - 1] - x:
r -= 1
else:
l += 1
return arr[l:r]
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 | class Solution {
public List<Integer> findClosestElements(int[] arr, int k, int x) {
int l = 0, r = arr.length;
while (r - l > k) {
if (x - arr[l] <= arr[r - 1] - x) {
--r;
} else {
++l;
}
}
List<Integer> ans = new ArrayList<>();
for (int i = l; i < r; ++i) {
ans.add(arr[i]);
}
return ans;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14 | class Solution {
public:
vector<int> findClosestElements(vector<int>& arr, int k, int x) {
int l = 0, r = arr.size();
while (r - l > k) {
if (x - arr[l] <= arr[r - 1] - x) {
--r;
} else {
++l;
}
}
return vector<int>(arr.begin() + l, arr.begin() + r);
}
};
|
| func findClosestElements(arr []int, k int, x int) []int {
l, r := 0, len(arr)
for r-l > k {
if x-arr[l] <= arr[r-1]-x {
r--
} else {
l++
}
}
return arr[l:r]
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13 | function findClosestElements(arr: number[], k: number, x: number): number[] {
let left = 0;
let right = arr.length - k;
while (left < right) {
const mid = (left + right) >> 1;
if (x - arr[mid] <= arr[mid + k] - x) {
right = mid;
} else {
left = mid + 1;
}
}
return arr.slice(left, left + k);
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 | impl Solution {
pub fn find_closest_elements(arr: Vec<i32>, k: i32, x: i32) -> Vec<i32> {
let k = k as usize;
let n = arr.len();
let mut left = 0;
let mut right = n - k;
while left < right {
let mid = left + (right - left) / 2;
if x - arr[mid] > arr[mid + k] - x {
left = mid + 1;
} else {
right = mid;
}
}
arr[left..left + k].to_vec()
}
}
|
方法三:二分查找
在方法二的基础上,我们更进一步,查找大小为 $k$ 的窗口的左边界。
时间复杂度 $O(logn)$。
| class Solution:
def findClosestElements(self, arr: List[int], k: int, x: int) -> List[int]:
left, right = 0, len(arr) - k
while left < right:
mid = (left + right) >> 1
if x - arr[mid] <= arr[mid + k] - x:
right = mid
else:
left = mid + 1
return arr[left : left + k]
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 | class Solution {
public List<Integer> findClosestElements(int[] arr, int k, int x) {
int left = 0;
int right = arr.length - k;
while (left < right) {
int mid = (left + right) >> 1;
if (x - arr[mid] <= arr[mid + k] - x) {
right = mid;
} else {
left = mid + 1;
}
}
List<Integer> ans = new ArrayList<>();
for (int i = left; i < left + k; ++i) {
ans.add(arr[i]);
}
return ans;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14 | class Solution {
public:
vector<int> findClosestElements(vector<int>& arr, int k, int x) {
int left = 0, right = arr.size() - k;
while (left < right) {
int mid = (left + right) >> 1;
if (x - arr[mid] <= arr[mid + k] - x)
right = mid;
else
left = mid + 1;
}
return vector<int>(arr.begin() + left, arr.begin() + left + k);
}
};
|
1
2
3
4
5
6
7
8
9
10
11
12 | func findClosestElements(arr []int, k int, x int) []int {
left, right := 0, len(arr)-k
for left < right {
mid := (left + right) >> 1
if x-arr[mid] <= arr[mid+k]-x {
right = mid
} else {
left = mid + 1
}
}
return arr[left : left+k]
}
|