We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent fee2a66 commit 3050edeCopy full SHA for 3050ede
scripts/algorithms/F/Find Kth Smallest Pair Distance/Find Kth Smallest Pair Distance.cpp
@@ -0,0 +1,18 @@
1
+// Runtime: 1285 ms (Top 5.12%) | Memory: 94.10 MB (Top 5.12%)
2
+
3
+class Solution {
4
+public:
5
+ int smallestDistancePair(vector<int>& nums, int k) {
6
+ int n = nums.size(), N = 1000000;
7
+ vector<int> cnt(N, 0);
8
+ for (int i = 0; i < n; i++) {
9
+ for (int j = i+1; j < n; j++)
10
+ cnt[abs(nums[i]-nums[j])]++;
11
+ }
12
+ for (int i = 0; i < N; i++) {
13
+ if (cnt[i] >= k) return i;
14
+ k -= cnt[i];
15
16
+ return 0;
17
18
+};
0 commit comments