Skip to content

Commit cd6632d

Browse files
committed
0719 updated.
1 parent db42308 commit cd6632d

File tree

2 files changed

+31
-29
lines changed

2 files changed

+31
-29
lines changed

0501-1000/0719-Find-K-th-Smallest-Pair-Distance/cpp-0719/main.cpp

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,58 @@
11
/// Source : https://leetcode.com/problems/find-k-th-smallest-pair-distance/description/
22
/// Author : liuyubobobo
3-
/// Time : 2017-10-28
3+
/// Time : 2022-06-14
44

55
#include <iostream>
66
#include <vector>
77
#include <algorithm>
8-
#include <set>
98

109
using namespace std;
1110

12-
/// Time Complexity: O(nlogn + n^2 + W)
13-
/// Space Complexity: O(W), where W = max(nums) - min(nums)
11+
12+
/// Binary Search
13+
/// Time Complexity: O(log(max_dis) * nlogn)
14+
/// Space Complexity: O(1)
1415
class Solution {
1516
public:
1617
int smallestDistancePair(vector<int>& nums, int k) {
1718

18-
int dis[1000000];
19-
for(int i = 0 ; i < 1000000 ; i ++)
20-
dis[i] = 0;
19+
int n = nums.size();
2120

2221
sort(nums.begin(), nums.end());
23-
for(int i = 0 ; i < nums.size() ; i ++)
24-
for(int j = i + 1 ; j < nums.size() ; j ++){
25-
//cout << nums[j] - nums[i] << endl;
26-
dis[nums[j]-nums[i]] ++;
27-
}
28-
29-
int index = 0;
30-
for(int i = 0 ; i < 1000000 ; i ++){
31-
index += dis[i];
32-
if(k <= index)
33-
return i;
22+
int l = 0, r = nums.back() - nums[0];
23+
while(l < r){
24+
int d = (l + r) / 2;
25+
if(dis_cnt_less_than_or_equal_to(n, nums, d) >= k) r = d;
26+
else l = d + 1;
3427
}
28+
return l;
29+
}
30+
31+
private:
32+
int dis_cnt_less_than_or_equal_to(int n, const vector<int>& nums, int d){
3533

36-
return -1;
34+
int cnt = 0;
35+
for(int i = 0; i < n; i ++){
36+
auto iter = upper_bound(nums.begin(), nums.end(), nums[i] + d);
37+
cnt += iter - nums.begin() - i - 1;
38+
}
39+
return cnt;
3740
}
3841
};
3942

43+
4044
int main() {
4145

42-
int nums1[] = {1, 3, 1};
43-
int k1 = 1;
44-
vector<int> vec1(nums1, nums1 + sizeof(nums1)/sizeof(int));
45-
cout << Solution().smallestDistancePair(vec1, k1) << endl;
46+
vector<int> nums1 = {1, 3, 1};
47+
cout << Solution().smallestDistancePair(nums1, 1) << endl;
4648
// 0
4749

48-
// ---
50+
vector<int> nums2 = {1, 1, 1};
51+
cout << Solution().smallestDistancePair(nums2, 2) << endl;
52+
// 0
4953

50-
int nums2[] = {1, 6, 1};
51-
int k2 = 3;
52-
vector<int> vec2(nums2, nums2 + sizeof(nums2)/sizeof(int));
53-
cout << Solution().smallestDistancePair(vec2, k2) << endl;
54+
vector<int> nums3 = {1, 6, 1};
55+
cout << Solution().smallestDistancePair(nums3, 3) << endl;
5456
// 5
5557

5658
return 0;

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
723723
| 716 | [Max Stack](https://leetcode.com/problems/max-stack/description/) | [solution](https://leetcode.com/problems/max-stack/solution/) | [C++](0501-1000/0716-Max-Stack/cpp-0716/) | | |
724724
| 717 | [1-bit and 2-bit Characters](https://leetcode.com/problems/1-bit-and-2-bit-characters/description/) | | [C++](0501-1000/0717-1-bit-and-2-bit-Characters/cpp-0717/) | | |
725725
| 718 | [Maximum Length of Repeated Subarray](https://leetcode.com/problems/maximum-length-of-repeated-subarray/description/) | [缺:Rolling Hash] | [C++](0501-1000/0718-Maximum-Length-of-Repeated-Subarray/cpp-0718/) | | |
726-
| 719 | [Find K-th Smallest Pair Distance](https://leetcode.com/problems/find-k-th-smallest-pair-distance/description/) | [缺:二分搜索] | [C++](0501-1000/0719-Find-K-th-Smallest-Pair-Distance/cpp-0719/) | | |
726+
| 719 | [Find K-th Smallest Pair Distance](https://leetcode.com/problems/find-k-th-smallest-pair-distance/description/) | [solution](https://leetcode.com/problems/find-k-th-smallest-pair-distance/solution/) | [C++](0501-1000/0719-Find-K-th-Smallest-Pair-Distance/cpp-0719/) | | |
727727
| 720 | [Longest Word in Dictionary](https://leetcode.com/problems/longest-word-in-dictionary/description/) | [solution](https://leetcode.com/problems/longest-word-in-dictionary/solution/) | [C++](0501-1000/0720-Longest-Word-in-Dictionary/cpp-0720/) | | |
728728
| 721 | [Accounts Merge](https://leetcode.com/problems/accounts-merge/description/) | [solution](https://leetcode.com/problems/accounts-merge/solution/) | [C++](0501-1000/0721-Accounts-Merge/cpp-0721/) | | |
729729
| 722 | [Remove Comments](https://leetcode.com/problems/remove-comments/description/) | [solution](https://leetcode.com/problems/remove-comments/solution/) | [C++](0501-1000/0722-Remove-Comments/cpp-0722/) | | |

0 commit comments

Comments
 (0)