719. Find K-th Smallest Pair Distance Hard

@problem@discussion
#Array#Two Pointers#Binary Search#Sorting



1/**
2 * [719] Find K-th Smallest Pair Distance
3 *
4 * The distance of a pair of integers a and b is defined as the absolute difference between a and b.
5 * Given an integer array nums and an integer k, return the k^th smallest distance among all the pairs nums[i] and nums[j] where 0 <= i < j < nums.length.
6 *  
7 * Example 1:
8 * 
9 * Input: nums = [1,3,1], k = 1
10 * Output: 0
11 * Explanation: Here are all the pairs:
12 * (1,3) -> 2
13 * (1,1) -> 0
14 * (3,1) -> 2
15 * Then the 1^st smallest distance pair is (1,1), and its distance is 0.
16 * 
17 * Example 2:
18 * 
19 * Input: nums = [1,1,1], k = 2
20 * Output: 0
21 * 
22 * Example 3:
23 * 
24 * Input: nums = [1,6,1], k = 3
25 * Output: 5
26 * 
27 *  
28 * Constraints:
29 * 
30 * 	n == nums.length
31 * 	2 <= n <= 10^4
32 * 	0 <= nums[i] <= 10^6
33 * 	1 <= k <= n * (n - 1) / 2
34 * 
35 */
36pub struct Solution {}
37
38// problem: https://leetcode.com/problems/find-k-th-smallest-pair-distance/
39// discuss: https://leetcode.com/problems/find-k-th-smallest-pair-distance/discuss/?currentPage=1&orderBy=most_votes&query=
40
41// submission codes start here
42
43impl Solution {
44    pub fn smallest_distance_pair(nums: Vec<i32>, k: i32) -> i32 {
45        0
46    }
47}
48
49// submission codes end
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54
55    #[test]
56    fn test_719() {
57    }
58}
59


Back
© 2025 bowen.ge All Rights Reserved.