Skip to content

Commit 458e910

Browse files
committed
Updates leetcode/binary-search/719-find-k-th-smallest-pair-distance.md
Auto commit by GitBook Editor
1 parent 9789965 commit 458e910

File tree

3 files changed

+95
-3
lines changed

3 files changed

+95
-3
lines changed

SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@
259259
* [704-Binary Search](leetcode/binary-search/704-binary-search.md)
260260
* [710-Random Pick with Blacklist](leetcode/binary-search/710-random-pick-with-blacklist.md)
261261
* [718-Maximum Length of Repeated Subarray](leetcode/binary-search/718-maximum-length-of-repeated-subarray.md)
262+
* [719-Find K-th Smallest Pair Distance](leetcode/binary-search/719-find-k-th-smallest-pair-distance.md)
262263
* [Template](template.md)
263264
* [LeetCode](template/leetcode.md)
264265
* [Other](other.md)

leetcode/binary-search/528-random-pick-with-weight.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
Given an array w of positive integers, where w\[i\] describes the weight of index i, write a function pickIndex which randomly picks an index in proportion to its weight.
66

7-
**Note:
7+
**Note:
88
**
99

1010
1. 1 <= w.length <= 10000
@@ -29,7 +29,7 @@ Input:
2929
Output: [null,0,1,1,1,0]
3030
```
3131

32-
**Explanation of Input Syntax:
32+
**Explanation of Input Syntax:
3333
**
3434

3535
The input is two lists: the subroutines called and their arguments. Solution's constructor has one argument, the array w. pickIndex has no arguments. Arguments are always wrapped with a list, even if there aren't any.
@@ -95,7 +95,7 @@ class Solution {
9595
map.put(total, i);
9696
}
9797
}
98-
98+
9999
public int pickIndex() {
100100
int k = map.higherKey(rand.nextInt(total));
101101
return map.get(k);

leetcode/binary-search/719-find-k-th-smallest-pair-distance.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
### Question {#question}
2+
3+
[https://leetcode.com/problems/find-k-th-smallest-pair-distance/description/](https://leetcode.com/problems/find-k-th-smallest-pair-distance/description/)
4+
5+
Given an integer array, return the k-th smallest distance among all the pairs. The distance of a pair \(A, B\) is defined as the absolute difference between A and B.
6+
7+
**Example:**
8+
9+
```
10+
Input:
11+
nums = [1,3,1]
12+
k = 1
13+
Output: 0
14+
Explanation:
15+
Here are all the pairs:
16+
(1,3) -> 2
17+
(1,1) -> 0
18+
(3,1) -> 2
19+
Then the 1st smallest distance pair is (1,1), and its distance is 0.
20+
```
21+
22+
**Note:**
23+
24+
1. 2 <= len\(nums\) <= 10000.
25+
2. 0 <= nums\[i\] < 1000000.
26+
3. 1 <= k <= len\(nums\) \* \(len\(nums\) - 1\) / 2.
27+
28+
### Thought Process {#thought-process}
29+
30+
1. Brute Force \(TLE\)
31+
1. Generate all the distance pairs through n^2 process
32+
2. Sort the distances and return the k-th distance by returning distance\[k - 1\]
33+
3. Time complexity O\(n^2\)
34+
4. Space complexity O\(n^2\)
35+
2. Heap \(TLE\)
36+
1. Sort the nums array and save all the adjacent pairs as nodes into the minHeap
37+
2. Every time, we poll the min distance pair out and save the next pair
38+
3. After looping for k times, the top node in the heap will contain the pair with kth distance
39+
4. Time complexity O\(nlogn + kLogn\)
40+
5. Space complexity O\(n\)
41+
3. asd
42+
43+
### Solution
44+
45+
```java
46+
class Solution {
47+
public int smallestDistancePair(int[] nums, int k) {
48+
int n = nums.length;
49+
int[] distances = new int[n * (n - 1) / 2];
50+
int z = 0;
51+
for (int i = 0; i < n; i++) {
52+
for (int j = i + 1; j < n; j++) {
53+
distances[z++] = Math.abs(nums[i] - nums[j]);
54+
}
55+
}
56+
Arrays.sort(distances);
57+
return distances[k - 1];
58+
}
59+
}
60+
```
61+
62+
```java
63+
class Solution {
64+
private static class Node {
65+
int i, j;
66+
public Node(int i, int j) {
67+
this.i = i;
68+
this.j = j;
69+
}
70+
}
71+
72+
public int smallestDistancePair(int[] nums, int k) {
73+
Arrays.sort(nums);
74+
PriorityQueue<Node> minHeap = new PriorityQueue<>((a, b) -> (nums[a.j] - nums[a.i]) - (nums[b.j] - nums[b.i]));
75+
for (int i = 0; i + 1 < nums.length; i++) {
76+
minHeap.offer(new Node(i, i + 1));
77+
}
78+
Node cur = null;
79+
while (k-- > 0) {
80+
cur = minHeap.poll();
81+
if (cur.j + 1 < nums.length) minHeap.offer(new Node(cur.i, cur.j + 1));
82+
}
83+
return nums[cur.j] - nums[cur.i];
84+
}
85+
}
86+
```
87+
88+
### Additional {#additional}
89+
90+
91+

0 commit comments

Comments
 (0)