-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package problem719 | ||
|
||
import ( | ||
"sort" | ||
) | ||
|
||
func smallestDistancePair(nums []int, k int) int { | ||
sort.Ints(nums) | ||
left, right := 0, nums[len(nums)-1]-nums[0] | ||
var mid int | ||
for left < right { | ||
mid = (left + right) / 2 | ||
cnt, j := 0, 0 | ||
for i, x := range nums { | ||
// 最关键的就是这里了,两个循环O(n^2)是过不了的 | ||
// 为什么i++之后,j不需要清0?小于j的都成立吗?是的。 | ||
// 因为nums[j-1]-nums[i]<=m,所以nums[j-1]-nums[i+1]<=m.(nums[i+1]>=nums[i]) | ||
for j < len(nums) && nums[j]-x <= mid { | ||
j++ | ||
} | ||
// 共有[i+1,j)个元素 | ||
// nums[i]不满足条件,因为不能自己减自己 | ||
// nums[j]不满足条件,因为是先加再判断的 | ||
cnt += j - i - 1 | ||
} | ||
if cnt < k { | ||
left = mid + 1 | ||
} else { | ||
right = mid | ||
} | ||
} | ||
return left | ||
} |