leetcode - 719. Find K-th Smallest Pair Distance

Description

The distance of a pair of integers a and b is defined as the absolute difference between a and b.

Given an integer array nums and an integer k, return the kth smallest distance among all the pairs nums[i] and nums[j] where 0 <= i < j < nums.length.

Example 1:

复制代码
Input: nums = [1,3,1], k = 1
Output: 0
Explanation: Here are all the pairs:
(1,3) -> 2
(1,1) -> 0
(3,1) -> 2
Then the 1st smallest distance pair is (1,1), and its distance is 0.

Example 2:

复制代码
Input: nums = [1,1,1], k = 2
Output: 0

Example 3:

复制代码
Input: nums = [1,6,1], k = 3
Output: 5

Constraints:

复制代码
n == nums.length
2 <= n <= 10^4
0 <= nums[i] <= 10^6
1 <= k <= n * (n - 1) / 2

Solution

Brute Force

Iterate all the possible pairs, then find the shortest distance. Use a max-root heap of size k to keep all the distance we have calculated.

Time complexity: o ( n 2 ) o(n^2) o(n2)

Space complexity: o ( k ) o(k) o(k)

The search range is [0, max(nums) - min(nums)], the difficult part is to find the monotonic part.

For binary search, we need to find a condition that, if condition(k) is True then condition(k + 1) is True.

Consider we have a given distance, there would be a point that, all the distance that is larger than the given distance have more k pairs, and all the distance that is smaller than given distance have less than k pairs.

复制代码
	[d1, d2, d3, d4, d5, d6...]
					  ^

If d5 is our target distance, then d6 and larger distances would have more than k pairs.

So we need a function, with the distance as input, and the output is bool, where it's True if input distance has k or more pairs, and False otherwise.

For this function, we could use 2 pointers. Image we have a fixed index i, and we have j where nums[j] - nums[i] <= distance, we keep moving j to right until nums[j] - nums[i] > distance, then we get j - i - 1 pairs.

Then we move i forward for 1 step, and check if nums[j] - nums[i] <= distance, if so, we keep moving j until it's not.

Keep this process until i reaches the end of the list.

Time complexity: o ( n log ⁡ n ) o(n\log n) o(nlogn), o ( n log ⁡ n ) o(n\log n) o(nlogn) for sorting and binary search, o ( n ) o(n) o(n) for 2 pointers.

Space complexity: o ( 1 ) o(1) o(1)

Code

Brute Force

python3 复制代码
class Solution:
    def smallestDistancePair(self, nums: List[int], k: int) -> int:
        k_small_distance = []
        for i in range(len(nums)):
            for j in range(i + 1, len(nums)):
                distance = abs(nums[i] - nums[j])
                heapq.heappush(k_small_distance, -distance)
                if len(k_small_distance) > k:
                    heapq.heappop(k_small_distance)
        return -min(k_small_distance)
python3 复制代码
class Solution:
    def smallestDistancePair(self, nums: List[int], k: int) -> int:
        def is_enough(distance: int) -> bool:
            """
            Returns:
                True: if there are k or more pairs with the distance or less
                False: if there are less than k pairs with the distance or less
            """
            count = 0
            i, j = 0, 0
            while i < len(nums) or j < len(nums):
                while j < len(nums) and nums[j] - nums[i] <= distance:
                    j += 1
                count += (j - i - 1)
                i += 1
            return count >= k

        nums.sort()
        left, right = 0, nums[-1] - nums[0]
        while left < right:
            mid = (left + right) >> 1
            if is_enough(mid):
                right = mid
            else:
                left = mid + 1
        return (left + right) >> 1
相关推荐
虾饺爱下棋35 分钟前
FCN语义分割算法原理与实战
人工智能·python·神经网络·算法
fouryears_234171 小时前
适配器模式——以springboot为例
java·spring boot·适配器模式
汽车功能安全啊3 小时前
利用对称算法及非对称算法实现安全启动
java·开发语言·安全
paopaokaka_luck3 小时前
基于Spring Boot+Vue的吉他社团系统设计和实现(协同过滤算法)
java·vue.js·spring boot·后端·spring
Eloudy4 小时前
简明量子态密度矩阵理论知识点总结
算法·量子力学
点云SLAM4 小时前
Eigen 中矩阵的拼接(Concatenation)与 分块(Block Access)操作使用详解和示例演示
人工智能·线性代数·算法·矩阵·eigen数学工具库·矩阵分块操作·矩阵拼接操作
Warren985 小时前
Java Stream流的使用
java·开发语言·windows·spring boot·后端·python·硬件工程
算法_小学生5 小时前
支持向量机(SVM)完整解析:原理 + 推导 + 核方法 + 实战
算法·机器学习·支持向量机
架构师沉默6 小时前
Java优雅使用Spring Boot+MQTT推送与订阅
java·开发语言·spring boot
tuokuac6 小时前
MyBatis 与 Spring Boot版本匹配问题
java·spring boot·mybatis