Skip to content

Commit 2ed53a3

Browse files
committed
feat: add solutions to lc problem: No.0658
No.0658.Find K Closest Elements
1 parent c2be5c8 commit 2ed53a3

File tree

6 files changed

+227
-24
lines changed

6 files changed

+227
-24
lines changed

solution/0600-0699/0658.Find K Closest Elements/README.md

+107-12
Original file line numberDiff line numberDiff line change
@@ -48,21 +48,23 @@
4848

4949
**方法一:排序**
5050

51-
`arr` 中的所有元素按照与 `x` 的距离从小到大进行排列。取前 `k` 个元素排序后返回。
51+
$arr$ 中的所有元素按照与 $x$ 的距离从小到大进行排列。取前 $k$ 个元素排序后返回。
5252

53-
时间复杂度$O(nlogn)$。
53+
时间复杂度 $O(nlogn)$。
5454

5555
**方法二:双指针**
5656

57-
根据 `arr` 有序的特点,可以声明头尾指针,然后根据 `k - arr[left]``arr[right] - k` 的判断结果缩小范围,直到 `right - left == k`
57+
直觉上,有序数组 $arr$ 最靠近 $x$ 的 $k$ 个数必然是一段连续的子数组。
5858

59-
时间复杂度:$O(n)$。
59+
我们可以声明头尾指针,记为 $l$ 和 $r$,然后根据 $x-arr[l]$ 与 $arr[r-1] - x$ 的大小比较结果缩小范围,直到 $r - l = k$。
60+
61+
时间复杂度 $O(n)$。
6062

6163
**方法三:二分查找**
6264

63-
查找大小为 `k` 的所有窗口的左边界
65+
在方法二的基础上,我们更进一步,查找大小为 $k$ 的窗口的左边界
6466

65-
时间复杂度$O(logn)$。
67+
时间复杂度 $O(logn)$。
6668

6769
<!-- tabs:start -->
6870

@@ -73,10 +75,22 @@
7375
```python
7476
class Solution:
7577
def findClosestElements(self, arr: List[int], k: int, x: int) -> List[int]:
76-
arr.sort(key=lambda v: (abs(v - x), x))
78+
arr.sort(key=lambda v: abs(v - x))
7779
return sorted(arr[:k])
7880
```
7981

82+
```python
83+
class Solution:
84+
def findClosestElements(self, arr: List[int], k: int, x: int) -> List[int]:
85+
l, r = 0, len(arr)
86+
while r - l > k:
87+
if x - arr[l] <= arr[r - 1] - x:
88+
r -= 1
89+
else:
90+
l += 1
91+
return arr[l: r]
92+
```
93+
8094
```python
8195
class Solution:
8296
def findClosestElements(self, arr: List[int], k: int, x: int) -> List[int]:
@@ -108,6 +122,26 @@ class Solution {
108122
}
109123
```
110124

125+
```java
126+
class Solution {
127+
public List<Integer> findClosestElements(int[] arr, int k, int x) {
128+
int l = 0, r = arr.length;
129+
while (r - l > k) {
130+
if (x - arr[l] <= arr[r - 1] - x) {
131+
--r;
132+
} else {
133+
++l;
134+
}
135+
}
136+
List<Integer> ans = new ArrayList<>();
137+
for (int i = l; i < r; ++i) {
138+
ans.add(arr[i]);
139+
}
140+
return ans;
141+
}
142+
}
143+
```
144+
111145
```java
112146
class Solution {
113147
public List<Integer> findClosestElements(int[] arr, int k, int x) {
@@ -152,13 +186,29 @@ public:
152186
};
153187
```
154188

189+
```cpp
190+
class Solution {
191+
public:
192+
vector<int> findClosestElements(vector<int>& arr, int k, int x) {
193+
int l = 0, r = arr.size();
194+
while (r - l > k) {
195+
if (x - arr[l] <= arr[r - 1] - x) {
196+
--r;
197+
} else {
198+
++l;
199+
}
200+
}
201+
return vector<int>(arr.begin() + l, arr.begin() + r);
202+
}
203+
};
204+
```
205+
155206
```cpp
156207
class Solution {
157208
public:
158209
vector<int> findClosestElements(vector<int>& arr, int k, int x) {
159210
int left = 0, right = arr.size() - k;
160-
while (left < right)
161-
{
211+
while (left < right) {
162212
int mid = (left + right) >> 1;
163213
if (x - arr[mid] <= arr[mid + k] - x) right = mid;
164214
else left = mid + 1;
@@ -192,6 +242,20 @@ func abs(x int) int {
192242
}
193243
```
194244

245+
```go
246+
func findClosestElements(arr []int, k int, x int) []int {
247+
l, r := 0, len(arr)
248+
for r-l > k {
249+
if x-arr[l] <= arr[r-1]-x {
250+
r--
251+
} else {
252+
l++
253+
}
254+
}
255+
return arr[l:r]
256+
}
257+
```
258+
195259
```go
196260
func findClosestElements(arr []int, k int, x int) []int {
197261
left, right := 0, len(arr)-k
@@ -209,16 +273,14 @@ func findClosestElements(arr []int, k int, x int) []int {
209273

210274
### **Rust**
211275

212-
双指针:
213-
214276
```rust
215277
impl Solution {
216278
pub fn find_closest_elements(arr: Vec<i32>, k: i32, x: i32) -> Vec<i32> {
217279
let n = arr.len();
218280
let mut l = 0;
219281
let mut r = n;
220282
while r - l != k as usize {
221-
if (arr[l] - x).abs() <= (arr[r - 1] - x).abs() {
283+
if x - arr[l] <= arr[r - 1] - x {
222284
r -= 1;
223285
} else {
224286
l += 1;
@@ -249,6 +311,39 @@ impl Solution {
249311
}
250312
```
251313

314+
### **TypeScript**
315+
316+
```ts
317+
function findClosestElements(arr: number[], k: number, x: number): number[] {
318+
let l = 0;
319+
let r = arr.length;
320+
while (r - l > k) {
321+
if (x - arr[l] <= arr[r - 1] - x) {
322+
--r;
323+
} else {
324+
++l;
325+
}
326+
}
327+
return arr.slice(l, r);
328+
}
329+
```
330+
331+
```ts
332+
function findClosestElements(arr: number[], k: number, x: number): number[] {
333+
let left = 0;
334+
let right = arr.length - k;
335+
while (left < right) {
336+
const mid = (left + right) >> 1;
337+
if (x - arr[mid] <= arr[mid + k] - x) {
338+
right = mid;
339+
} else {
340+
left = mid + 1;
341+
}
342+
}
343+
return arr.slice(left, left + k);
344+
}
345+
```
346+
252347
### **...**
253348

254349
```

solution/0600-0699/0658.Find K Closest Elements/README_EN.md

+99-4
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,22 @@
4444
```python
4545
class Solution:
4646
def findClosestElements(self, arr: List[int], k: int, x: int) -> List[int]:
47-
arr.sort(key=lambda v: (abs(v - x), x))
47+
arr.sort(key=lambda v: abs(v - x))
4848
return sorted(arr[:k])
4949
```
5050

51+
```python
52+
class Solution:
53+
def findClosestElements(self, arr: List[int], k: int, x: int) -> List[int]:
54+
l, r = 0, len(arr)
55+
while r - l > k:
56+
if x - arr[l] <= arr[r - 1] - x:
57+
r -= 1
58+
else:
59+
l += 1
60+
return arr[l: r]
61+
```
62+
5163
```python
5264
class Solution:
5365
def findClosestElements(self, arr: List[int], k: int, x: int) -> List[int]:
@@ -77,6 +89,26 @@ class Solution {
7789
}
7890
```
7991

92+
```java
93+
class Solution {
94+
public List<Integer> findClosestElements(int[] arr, int k, int x) {
95+
int l = 0, r = arr.length;
96+
while (r - l > k) {
97+
if (x - arr[l] <= arr[r - 1] - x) {
98+
--r;
99+
} else {
100+
++l;
101+
}
102+
}
103+
List<Integer> ans = new ArrayList<>();
104+
for (int i = l; i < r; ++i) {
105+
ans.add(arr[i]);
106+
}
107+
return ans;
108+
}
109+
}
110+
```
111+
80112
```java
81113
class Solution {
82114
public List<Integer> findClosestElements(int[] arr, int k, int x) {
@@ -121,13 +153,29 @@ public:
121153
};
122154
```
123155

156+
```cpp
157+
class Solution {
158+
public:
159+
vector<int> findClosestElements(vector<int>& arr, int k, int x) {
160+
int l = 0, r = arr.size();
161+
while (r - l > k) {
162+
if (x - arr[l] <= arr[r - 1] - x) {
163+
--r;
164+
} else {
165+
++l;
166+
}
167+
}
168+
return vector<int>(arr.begin() + l, arr.begin() + r);
169+
}
170+
};
171+
```
172+
124173
```cpp
125174
class Solution {
126175
public:
127176
vector<int> findClosestElements(vector<int>& arr, int k, int x) {
128177
int left = 0, right = arr.size() - k;
129-
while (left < right)
130-
{
178+
while (left < right) {
131179
int mid = (left + right) >> 1;
132180
if (x - arr[mid] <= arr[mid + k] - x) right = mid;
133181
else left = mid + 1;
@@ -161,6 +209,20 @@ func abs(x int) int {
161209
}
162210
```
163211

212+
```go
213+
func findClosestElements(arr []int, k int, x int) []int {
214+
l, r := 0, len(arr)
215+
for r-l > k {
216+
if x-arr[l] <= arr[r-1]-x {
217+
r--
218+
} else {
219+
l++
220+
}
221+
}
222+
return arr[l:r]
223+
}
224+
```
225+
164226
```go
165227
func findClosestElements(arr []int, k int, x int) []int {
166228
left, right := 0, len(arr)-k
@@ -185,7 +247,7 @@ impl Solution {
185247
let mut l = 0;
186248
let mut r = n;
187249
while r - l != k as usize {
188-
if (arr[l] - x).abs() <= (arr[r - 1] - x).abs() {
250+
if x - arr[l] <= arr[r - 1] - x {
189251
r -= 1;
190252
} else {
191253
l += 1;
@@ -216,6 +278,39 @@ impl Solution {
216278
}
217279
```
218280

281+
### **TypeScript**
282+
283+
```ts
284+
function findClosestElements(arr: number[], k: number, x: number): number[] {
285+
let l = 0;
286+
let r = arr.length;
287+
while (r - l > k) {
288+
if (x - arr[l] <= arr[r - 1] - x) {
289+
--r;
290+
} else {
291+
++l;
292+
}
293+
}
294+
return arr.slice(l, r);
295+
}
296+
```
297+
298+
```ts
299+
function findClosestElements(arr: number[], k: number, x: number): number[] {
300+
let left = 0;
301+
let right = arr.length - k;
302+
while (left < right) {
303+
const mid = (left + right) >> 1;
304+
if (x - arr[mid] <= arr[mid + k] - x) {
305+
right = mid;
306+
} else {
307+
left = mid + 1;
308+
}
309+
}
310+
return arr.slice(left, left + k);
311+
}
312+
```
313+
219314
### **...**
220315

221316
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function findClosestElements(arr: number[], k: number, x: number): number[] {
2+
let left = 0;
3+
let right = arr.length - k;
4+
while (left < right) {
5+
const mid = (left + right) >> 1;
6+
if (x - arr[mid] <= arr[mid + k] - x) {
7+
right = mid;
8+
} else {
9+
left = mid + 1;
10+
}
11+
}
12+
return arr.slice(left, left + k);
13+
}

solution/CONTEST_README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
| 段位 | 比例 | 段位名 | 国服分数线 | 勋章 |
1313
| ----- | ------ | -------- | --------- | --------------------------------------------------------------------------- |
14-
| LV3 | 5% | Guardian | &ge;2221.50 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Guardian.gif" style="width: 80px;" /></p> |
15-
| LV2 | 20% | Knight | &ge;1867.03 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Knight.gif" style="width: 80px;" /></p> |
14+
| LV3 | 5% | Guardian | &ge;2221.84 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Guardian.gif" style="width: 80px;" /></p> |
15+
| LV2 | 20% | Knight | &ge;1867.64 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Knight.gif" style="width: 80px;" /></p> |
1616
| LV1 | 75% | - | - | - |
1717

1818
力扣竞赛 **全国排名前 10** 的用户,全站用户名展示为品牌橙色。

solution/CONTEST_README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ If you are in the top 25% of the contest rating, you’ll get the “Knight” b
1313

1414
| Level | Proportion | Badge | Rating | |
1515
| ----- | ---------- | ---------- | -------------- | ----------------------------------------------------------------------------------------------------------------------- |
16-
| LV3 | 5\% | Guardian | &ge;2172.98 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Guardian.gif" style="width: 80px;" /></p> |
17-
| LV2 | 20\% | Knight | &ge;1847.67 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Knight.gif" style="width: 80px;" /></p> |
16+
| LV3 | 5\% | Guardian | &ge;2173.79 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Guardian.gif" style="width: 80px;" /></p> |
17+
| LV2 | 20\% | Knight | &ge;1848.62 | <p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/images/Knight.gif" style="width: 80px;" /></p> |
1818
| LV1 | 75\% | - | - | - |
1919

2020
For top 10 users (excluding LCCN users), your LeetCode ID will be colored orange on the ranking board. You'll also have the honor with you when you post/comment under discuss.

0 commit comments

Comments
 (0)