Skip to content

Commit 50de4b4

Browse files
authored
Create 719. Find K-th Smallest Pair Distance (#557)
2 parents c61c5f7 + 5e85842 commit 50de4b4

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

719. Find K-th Smallest Pair Distance

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public:
3+
int smallestDistancePair(vector<int>& nums, int k) {
4+
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
5+
const int N=1e6+1;
6+
int mp[N]={0};
7+
sort(nums.begin(),nums.end());
8+
int n=nums.size();
9+
for (int i=0;i<n;i++){
10+
for (int j=i+1;j<n;j++){
11+
mp[abs(nums[i]-nums[j])]++;
12+
}
13+
}
14+
int ct=0;
15+
int diff=0;
16+
for (int i=0;i<N;i++){
17+
cout<<mp[i]<<endl;
18+
ct+=mp[i];
19+
if (ct>=k){
20+
diff=i;
21+
break;
22+
}
23+
}
24+
return diff++;
25+
}
26+
};

0 commit comments

Comments
 (0)