-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path0719.cpp
More file actions
31 lines (25 loc) · 715 Bytes
/
Copy path0719.cpp
File metadata and controls
31 lines (25 loc) · 715 Bytes
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
class Solution {
int count(const vector<int> &nums, int tgt) const {
int res = 0;
for (int l = 0, r = 1; r < size(nums); r++) {
while (nums[r] - nums[l] > tgt)
l++;
res += r - l;
}
return res;
}
public:
int smallestDistancePair(vector<int> &nums, int k) const {
sort(begin(nums), end(nums));
int low = 0, high = nums.back() - nums.front();
while (low < high) {
const int mid = low + (high - low) / 2;
const int cnt = count(nums, mid);
if (cnt < k)
low = mid + 1;
else
high = mid;
}
return low;
}
};