Skip to content

feat: further improve sliding window search #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions leetcode/binary-search/719-find-k-th-smallest-pair-distance.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,22 +101,25 @@ class Solution {
int lo = 0, hi = nums[nums.length - 1] - nums[0];
while (lo != hi) {
int mi = lo + (hi - lo) / 2;
if (count(nums, mi) < k) lo = mi + 1;
if (isCountBelowThreshold(nums, mi, k)) lo = mi + 1;
else hi = mi;
}
return lo;
}

private int count(int[] nums, int d) {
private boolean isCountBelowThreshold(int[] nums, int d, int k) {
int right = 1;
int count = 0;
while (right < nums.length) {
int left = 0;
while (nums[right] - nums[left] > d) left++;
count += right - left;
right++;
int left = 0;
while (right < nums.length && count < k) {
if (nums[right] - nums[left] > d) {
++left;
} else {
count += right - left;
++right;
}
}
return count;
return count < k;
}
}
```
Expand Down