- Find K Closest Elements
Given target, a non-negative integer k and an integer array A sorted in ascending order, find the k closest numbers to target in A, sorted in ascending order by the difference between the number and target. Otherwise, sorted in ascending order by number if the difference is same.
Example
Example 1:
Input: A = [1, 2, 3], target = 2, k = 3
Output: [2, 1, 3]
Example 2:
Input: A = [1, 4, 6, 8], target = 3, k = 3
Output: [4, 1, 6]
Challenge
O(logn + k) time
Notice
The value k is a non-negative integer and will always be smaller than the length of the sorted array.
Length of the given array is positive and will not exceed 10^410
4
Absolute value of elements in the array will not exceed 10^410
4
解法1:
先用searchInsert()找到应该插入的位置,然后用两个指针从中间往前后找,直到找到K个数。
代码如下:
int searchInsert(vector<int>& nums, int target) {
if (nums.size()==0) return -1;
int start=0, end=nums.size()-1;
while(start+1<end) {
int mid=start+(end-start)/2;
if (nums[mid]==target)
return mid;
else if (nums[mid]<target)
start=mid;
else
end=mid;
}
if (nums[start]>=target) return start;
if (nums[end]<target) return end+1;
return end;
}
vector<int> kClosestNumbers(vector<int> &A, int target, int k) {
vector<int> result;
if (k>A.size()) return result;
int pos = searchInsert(A, target);
int p1, p2;
if (pos == 0) {
p1=0; p2=1;
} else {//if (pos == A.size()) {
p1=pos-1; p2=pos;
}
int count=0;
while(count<k && p1>=0 && p2<A.size()) {
if ((target-A[p1]) <= (A[p2]-target)) {//note <=here
result.push_back(A[p1--]);
} else {
result.push_back(A[p2++]);
}
count++;
}
while(count<k) {
if (p1<0) {
result.push_back(A[p2++]);
} else {
result.push_back(A[p1--]);
}
count++;
}
return result;
}
注意:
- 当一边已经找完则剩下的全部从另一边找
- 当insertPos在边界上时要注意。
Some test cases:
- test case 1:
[1,4,6,10,20]
21
4 - test case 2:
[1,10,15,25,35,45,50,59]
30
7
二刷:
class Solution {
public:
/**
* @param a: an integer array
* @param target: An integer
* @param k: An integer
* @return: an integer array
*/
vector<int> kClosestNumbers(vector<int> &a, int target, int k) {
vector<int> res;
int start = 0, end = a.size();
while (start + 1 < end) {
int mid = start + (end - start) / 2;
if (a[mid] < target) {
start = mid;
} else {
end = mid;
}
}
int pos;
if (target - a[start] <= a[end] - target) pos = start;
else pos = end;
int numElems = 0;
int left = pos, right = pos + 1;
while (numElems < k) {
if (left >= 0 && right <= a.size() - 1) {
if (target - a[left] <= a[right] - target) {
res.push_back(a[left]);
left--;
} else {
res.push_back(a[right]);
right++;
}
} else if (left >= 0) {
res.push_back(a[left]);
left--;
} else {
res.push_back(a[right]);
right++;
}
numElems++;
}
return res;
}
};
三刷:
没有用二分法,直接用左右两个指针一左一右向中间靠近,直到两个指针中间距离为k。这个效率低些。
class Solution {
public:
/**
* @param a: an integer array
* @param target: An integer
* @param k: An integer
* @return: an integer array
*/
vector<int> kClosestNumbers(vector<int> &a, int target, int k) {
int len = a.size();
int start = 0, end = len - 1;
while (end - start + 1 > k) {
if (abs(a[start] - target) <= abs(a[end] - target)) {
end--;
} else {
start++;
}
}
vector<int> res(k, 0);
while (k > 0) {
if (abs(a[start] - target) > abs(a[end] - target)) {
res[k - 1] = a[start];
start++;
} else {
res[k - 1] = a[end];
end--;
}
k--;
}
return res;
}
};
四刷:注意用二分法是先找到最左侧的那个等于x的数,如果找不到就在start和end中找更接近x的那个。
为什么是最左侧?Otherwise, sorted in ascending order by number if the difference is same.
class Solution {
public:
vector<int> findClosestElements(vector<int>& arr, int k, int x) {
int n = arr.size();
int start = 0, end = n - 1, count = 0;
while (start + 1 < end) {
int mid = start + (end - start) / 2;
if (arr[mid] < x) {
start = mid;
} else {
end = mid;
}
}
int pos = start;
if (abs(arr[start] - x) < abs(arr[end] - x)) pos = start;
else pos = end;
vector<int> res;
start = pos - 1, end = pos;
while (count < k && start >= 0 && end < n) {
if (abs(arr[start] - x) <= abs(arr[end] - x)) {
res.push_back(arr[start]);
start--;
} else {
res.push_back(arr[end]);
end++;
}
count++;
}
while (count < k && start >= 0) {
res.push_back(arr[start--]);
count++;
}
while (count < k && end < n) {
res.push_back(arr[end++]);
count++;
}
sort(res.begin(), res.end());
return res;
}
};
本文介绍了一种高效算法,用于从升序排列的数组中找出与目标值最接近的K个数,并按与目标值的差值或数值大小进行排序。通过二分查找定位起始位置,结合双指针技巧实现。
631





