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
相关推荐
Daniel 大东11 分钟前
BugJson因为json格式问题OOM怎么办
java·安全
不去幼儿园13 分钟前
【MARL】深入理解多智能体近端策略优化(MAPPO)算法与调参
人工智能·python·算法·机器学习·强化学习
Mr_Xuhhh15 分钟前
重生之我在学环境变量
linux·运维·服务器·前端·chrome·算法
盼海1 小时前
排序算法(五)--归并排序
数据结构·算法·排序算法
Theodore_10224 小时前
4 设计模式原则之接口隔离原则
java·开发语言·设计模式·java-ee·接口隔离原则·javaee
网易独家音乐人Mike Zhou5 小时前
【卡尔曼滤波】数据预测Prediction观测器的理论推导及应用 C语言、Python实现(Kalman Filter)
c语言·python·单片机·物联网·算法·嵌入式·iot
冰帝海岸5 小时前
01-spring security认证笔记
java·笔记·spring
世间万物皆对象6 小时前
Spring Boot核心概念:日志管理
java·spring boot·单元测试
没书读了6 小时前
ssm框架-spring-spring声明式事务
java·数据库·spring
小二·6 小时前
java基础面试题笔记(基础篇)
java·笔记·python