Skip to content

Commit 00690a2

Browse files
committed
✨feat: add 658
1 parent 723b6da commit 00690a2

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

Index/二分.md

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
| [532. 数组中的 k-diff 数对](https://leetcode.cn/problems/k-diff-pairs-in-an-array/) | [LeetCode 题解链接](https://leetcode.cn/problems/k-diff-pairs-in-an-array/solution/by-ac_oier-ap3v/) | 中等 | 🤩🤩🤩🤩 |
3333
| [540. 有序数组中的单一元素](https://leetcode-cn.com/problems/single-element-in-a-sorted-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/single-element-in-a-sorted-array/solution/gong-shui-san-xie-er-duan-xing-fen-xi-yu-17nv/) | 中等 | 🤩🤩🤩🤩 |
3434
| [611. 有效三角形的个数](https://leetcode-cn.com/problems/valid-triangle-number/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/valid-triangle-number/solution/gong-shui-san-xie-yi-ti-san-jie-jian-dan-y1we/) | 中等 | 🤩🤩🤩🤩 |
35+
| [658. 找到 K 个最接近的元素](https://leetcode.cn/problems/find-k-closest-elements/) | [LeetCode 题解链接](https://leetcode.cn/problems/find-k-closest-elements/solution/by-ac_oier-8xh5/) | 中等 | 🤩🤩🤩🤩 |
3536
| [668. 乘法表中第k小的数](https://leetcode.cn/problems/kth-smallest-number-in-multiplication-table/) | [LeetCode 题解链接](https://leetcode.cn/problems/kth-smallest-number-in-multiplication-table/solution/by-ac_oier-7pmt/) | 困难 | 🤩🤩🤩🤩🤩 |
3637
| [704. 二分查找](https://leetcode-cn.com/problems/binary-search/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/binary-search/solution/gong-shui-san-xie-yun-yong-er-fen-zhao-f-5jyj/) | 简单 | 🤩🤩🤩🤩🤩 |
3738
| [710. 黑名单中的随机数](https://leetcode.cn/problems/random-pick-with-blacklist/) | [LeetCode 题解链接](https://leetcode.cn/problems/random-pick-with-blacklist/solution/by-ac_oier-2rww/) | 困难 | 🤩🤩🤩🤩 |

Index/双指针.md

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
| [633. 平方数之和](https://leetcode-cn.com/problems/sum-of-square-numbers/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/sum-of-square-numbers/solution/gong-shui-san-xie-yi-ti-san-jie-mei-ju-s-7qi5/) | 简单 | 🤩🤩 |
3131
| [640. 求解方程](https://leetcode.cn/problems/solve-the-equation/) | [LeetCode 题解链接](https://leetcode.cn/problems/solve-the-equation/solution/by-ac_oier-fvee/) | 中等 | 🤩🤩🤩🤩 |
3232
| [653. 两数之和 IV - 输入 BST](https://leetcode-cn.com/problems/two-sum-iv-input-is-a-bst/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/two-sum-iv-input-is-a-bst/solution/by-ac_oier-zr4o/) | 简单 | 🤩🤩🤩🤩 |
33+
| [658. 找到 K 个最接近的元素](https://leetcode.cn/problems/find-k-closest-elements/) | [LeetCode 题解链接](https://leetcode.cn/problems/find-k-closest-elements/solution/by-ac_oier-8xh5/) | 中等 | 🤩🤩🤩🤩 |
3334
| [719. 找出第 K 小的数对距离](https://leetcode.cn/problems/find-k-th-smallest-pair-distance/) | [LeetCode 题解链接](https://leetcode.cn/problems/find-k-th-smallest-pair-distance/solution/by-ac_oier-o4if/) | 困难 | 🤩🤩🤩🤩 |
3435
| [786. 第 K 个最小的素数分数](https://leetcode-cn.com/problems/k-th-smallest-prime-fraction/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/k-th-smallest-prime-fraction/solution/gong-shui-san-xie-yi-ti-shuang-jie-you-x-8ymk/) | 中等 | 🤩🤩🤩 |
3536
| [825. 适龄的朋友](https://leetcode-cn.com/problems/friends-of-appropriate-ages/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/friends-of-appropriate-ages/solution/gong-shui-san-xie-yi-ti-shuang-jie-pai-x-maa8/) | 中等 | 🤩🤩🤩🤩 |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[658. 找到 K 个最接近的元素](https://leetcode.cn/problems/find-k-closest-elements/solution/by-ac_oier-8xh5/)** ,难度为 **中等**
4+
5+
Tag : 「二分」、「双指针」
6+
7+
8+
9+
给定一个 排序好 的数组 `arr`,两个整数 `k``x` ,从数组中找到最靠近 `x`(两数之差最小)的 `k` 个数。返回的结果必须要是按升序排好的。
10+
11+
整数 `a` 比整数 `b` 更接近 `x` 需要满足:
12+
13+
* `|a - x| < |b - x|` 或者
14+
* `|a - x| == |b - x|``a < b`
15+
16+
示例 1:
17+
```
18+
输入:arr = [1,2,3,4,5], k = 4, x = 3
19+
20+
输出:[1,2,3,4]
21+
```
22+
示例 2:
23+
```
24+
输入:arr = [1,2,3,4,5], k = 4, x = -1
25+
26+
输出:[1,2,3,4]
27+
```
28+
29+
提示:
30+
* $1 <= k <= arr.length$
31+
* $1 <= arr.length <= 10^4$
32+
* `arr` 按升序排列
33+
* $-10^4 <= arr[i], x <= 10^4$
34+
35+
---
36+
37+
### 二分 + 双指针
38+
39+
容易想到先通过「二分」找到与 `x` 差值最小的位置 `idx`,然后从 `idx` 开始使用「双指针」往两边进行拓展(初始化左端点 $i = idx - 1$,右端点 $j = idx + 1$),含义为 $[i + 1, j - 1]$ 范围内子数组为候选区间,不断根据两边界与 `x` 的差值关系进行扩充,直到候选区间包含 $k$ 个数。
40+
41+
Java 代码:
42+
```Java
43+
class Solution {
44+
public List<Integer> findClosestElements(int[] arr, int k, int x) {
45+
int n = arr.length, l = 0, r = n - 1;
46+
while (l < r) {
47+
int mid = l + r + 1 >> 1;
48+
if (arr[mid] <= x) l = mid;
49+
else r = mid - 1;
50+
}
51+
r = r + 1 < n && Math.abs(arr[r + 1] - x) < Math.abs(arr[r] - x) ? r + 1 : r;
52+
int i = r - 1, j = r + 1;
53+
while (j - i - 1 < k) {
54+
if (i >= 0 && j < n) {
55+
if (Math.abs(arr[j] - x) < Math.abs(arr[i] - x)) j++;
56+
else i--;
57+
} else if (i >= 0) {
58+
i--;
59+
} else {
60+
j++;
61+
}
62+
}
63+
List<Integer> ans = new ArrayList<>();
64+
for (int p = i + 1; p <= j - 1; p++) ans.add(arr[p]);
65+
return ans;
66+
}
67+
}
68+
```
69+
TypeScript 代码:
70+
```TypeScript
71+
function findClosestElements(arr: number[], k: number, x: number): number[] {
72+
let n = arr.length, l = 0, r = n - 1
73+
while (l < r) {
74+
const mid = l + r + 1 >> 1
75+
if (arr[mid] <= x) l = mid
76+
else r = mid - 1
77+
}
78+
r = r + 1 < n && Math.abs(arr[r + 1] - x) < Math.abs(arr[r] - x) ? r + 1 : r
79+
let i = r - 1, j = r + 1
80+
while (j - i - 1 < k) {
81+
if (i >= 0 && j < n) {
82+
if (Math.abs(arr[j] - x) < Math.abs(arr[i] - x)) j++
83+
else i--
84+
} else if (i >= 0) {
85+
i--
86+
} else {
87+
j++
88+
}
89+
}
90+
return arr.slice(i + 1, j);
91+
};
92+
```
93+
* 时间复杂度:查找分割点复杂度为 $O(\log{n})$;从分割点往两边拓展复杂度为 $O(k)$。整体复杂度为 $O(\log{n} + k)$
94+
* 空间复杂度:$O(1)$
95+
96+
---
97+
98+
### 最后
99+
100+
这是我们「刷穿 LeetCode」系列文章的第 `No.658` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
101+
102+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
103+
104+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
105+
106+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
107+

0 commit comments

Comments
 (0)