Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f9122c0

Browse files
Time: 86 ms (79.14%), Space: 17.5 MB (16.16%) - LeetHub
1 parent 8a35586 commit f9122c0

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution:
2+
def countPairs(self, nums, mid):
3+
count = 0
4+
left = 0
5+
for right in range(len(nums)):
6+
while nums[right] - nums[left] > mid:
7+
left += 1
8+
count += right - left
9+
return count
10+
def smallestDistancePair(self, nums: List[int], k: int) -> int:
11+
nums.sort() # Sort the array first
12+
low = 0
13+
high = nums[-1] - nums[0]
14+
15+
while low < high:
16+
mid = (low + high) // 2
17+
if self.countPairs(nums, mid) < k:
18+
low = mid + 1
19+
else:
20+
high = mid
21+
22+
return low

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /