719-Find K-th Smallest Pair Distance

标签: 算法 数据结构 leetcode
10人阅读 评论(0) 收藏 举报
分类:

Description:
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.


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.

Note:

  1. 2 <= len(nums) <= 10000.
  2. 0 <= nums[i] < 1000000.
  3. 1 <= k <= len(nums) * (len(nums) - 1) / 2.

问题描述
给定整数数组,返回所有对中的第k小的距离。(A, B)对的距离为A和B的差的绝对值。


问题分析
这种题目的套路是找出上下界,以某种标准(或者计数方式)通过二分查找求解。如果对这种类型的题目不是很了解的话,可以看看这个链接,总结得非常详细:
https://leetcode.com/problems/k-th-smallest-prime-fraction/discuss/115819/Summary-of-solutions-for-problems-%22reducible%22-to-LeetCode-378


解法

class Solution {
    public int smallestDistancePair(int[] nums, int k) {
        //注意这里,对数组进行排序处理
        Arrays.sort(nums);

        int len = nums.length;

        int left = 0, right = nums[len - 1] - nums[0];

        while(left < right){
            int mid = left + (right - left) / 2;
            int count = getCount(nums, mid);
            //注意这里的二分查找左边界和右边界更新的标准
            //小于k,mid必然非最终解,因此left = mid + 1
            //大于等于k,可以将mid作为候选解
            if(count < k)   left = mid + 1;
            else            right = mid;
        }

        return left;
    }
    //计数方式
    public int getCount(int[] nums, int mid){
        int count = 0;

        int left = 0;
        //注意这里的left,在迭代的过程中没有重置,而是一直右移
        //很容易理解,i右移,nums[i]变大(注意对数组进行过排序),而nums[left](上一轮的)不变
        //因此差增大,必然还是比mid大,因此不需要再迭代一遍
        for(int i = 1; i < nums.length;i++){
            while(nums[i] - nums[left] > mid)   left++;
            count += i - left;
        }

        return count;
    }
}
查看评论

[Leetcode] 719. Find K-th Smallest Pair Distance 解题报告

题目: Given an integer array, return the k-th smallest distance among all the pairs. The distance...
  • magicbean2
  • magicbean2
  • 2018-02-15 14:47:48
  • 85

719. Find K-th Smallest Pair Distance

Given an integer array, return the k-th smallest distance among all the pairs. The distance of a pai...
  • zjucor
  • zjucor
  • 2017-10-29 11:32:59
  • 827

LWC 56:719. Find K-th Smallest Pair Distance

LWC 56:719. Find K-th Smallest Pair Distance传送门:719. Find K-th Smallest Pair DistanceProblem: Give...
  • u014688145
  • u014688145
  • 2017-10-29 14:21:58
  • 1343

leetcode 719. Find K-th Smallest Pair Distance

719. Find K-th Smallest Pair Distance Given an integer array, return the k-th smallest distance...
  • liqinzhe11
  • liqinzhe11
  • 2017-11-28 10:07:41
  • 455

LeetCode Find K-th Smallest Pair Distance

Given an integer array, return the k-th smallest distance among all the pairs. The distance of a pai...
  • qq_24153697
  • qq_24153697
  • 2017-12-04 16:52:06
  • 54

leetcode 719. Find K-th Smallest Pair Distance 第k小的绝对距离 + 暴力计算真棒

Given an integer array, return the k-th smallest distance among all the pairs. The distance of a pai...
  • JackZhang_123
  • JackZhang_123
  • 2017-12-25 15:34:56
  • 84

Find K-th Smallest Pair Distance:查找数组元素中差值第K大的两个元素的差值

Given an integer array, return the k-th smallest distance among all the pairs. The distance of a pai...
  • u013300579
  • u013300579
  • 2017-10-30 17:29:53
  • 568

算法第15周Find K-th Smallest Pair Distance[hard]

DescriptionGiven an integer array, return the k-th smallest distance among all the pairs. The distan...
  • south_l
  • south_l
  • 2017-12-16 00:12:28
  • 75

K-th Smallest in Lexicographical Order

依次逐渐确定前缀,每次计算当前前缀下不超过n的个数,若个数超过k,则说明第k个数在当前前缀下,反之,则说明在当前前缀加1以后的前缀; 以first为前缀的数 [first,first+1] ...
  • popvip44
  • popvip44
  • 2017-01-11 14:53:45
  • 199

LeetCode719. Find K-th Smallest Pair Distance (hard)

Given an integer array, return the k-th smallest distance among all the pairs. The distance of a pai...
  • Lollipop66
  • Lollipop66
  • 2018-03-09 17:46:03
  • 12
    个人资料
    专栏达人 持之以恒
    等级:
    访问量: 2万+
    积分: 6529
    排名: 4582
    博客专栏
    文章存档
    最新评论