|
| 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