LeetCode719. Find K-th Smallest Pair Distance——二分答案

这篇具有很好参考价值的文章主要介绍了LeetCode719. Find K-th Smallest Pair Distance——二分答案。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一、题目

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 <= 104
0 <= nums[i] <= 106
1 <= k <= n * (n - 1) / 2

二、题解

答案在0到最大差值之间,二分判断文章来源地址https://www.toymoban.com/news/detail-822964.html

class Solution {
public:
    int smallestDistancePair(vector<int>& nums, int k) {
        int n = nums.size();
        int res = 0;
        sort(nums.begin(),nums.end());
        int l = 0,r = nums[n - 1] - nums[0];
        while(l <= r){
            int mid = l + ((r - l) >> 1);
            if(f(nums,mid) >= k){
                res = mid;
                r = mid - 1;
            }
            else l = mid + 1;
        }
        return res;
    }
    //距离小于limit的数对个数
    int f(vector<int>& nums,int limit){
        int n = nums.size();
        int count = 0;
        for(int l = 0,r = 0;l < n;l++){
            while(r + 1 < n && nums[r + 1] - nums[l] <= limit) r++;
            count += r - l;
        }
        return count;
    }
};

到了这里,关于LeetCode719. Find K-th Smallest Pair Distance——二分答案的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • LeetCode 865. Smallest Subtree with all the Deepest Nodes【树,DFS,BFS,哈希表】1534

    本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,

    2024年02月09日
    浏览(31)
  • C++二分算法(二分查找&二分答案)细节详解

     二分算法可以分为 二分查找 和 二分答案 。 以在一个 升序数组 中查找一个数为例。它每次考察数组当前部分的 中间元素 ,如果中间元素刚好是要找的,就结束搜索过程;如果中间元素小于所查找的值,那么左侧的只会更小,不会有所查找的元素,只需到右侧查找;如果

    2024年02月08日
    浏览(39)
  • LeetCode646. Maximum Length of Pair Chain——动态规划

    You are given an array of n pairs pairs where pairs[i] = [lefti, righti] and lefti righti. A pair p2 = [c, d] follows a pair p1 = [a, b] if b c. A chain of pairs can be formed in this fashion. Return the length longest chain which can be formed. You do not need to use up all the given intervals. You can select pairs in any order. Example 1: Input: pairs = [[

    2024年02月22日
    浏览(30)
  • Python数据结构与算法篇(五)-- 二分查找与二分答案

    1.1 定义         二分查找又称折半查找、二分搜索、折半搜索等,是一种在静态查找表中查找特定元素的算法。         所谓静态查找表,即只能对表内的元素做查找和读取操作,不允许插入或删除元素。         使用二分查找算法,必须保证查找表中存放的是有

    2023年04月20日
    浏览(37)
  • 【二分答案】CF1661 C

    Problem - C - Codeforces 题意: 思路: 在check的时候,我们要尽量用算贡献的思想,并且大胆贪心 Code:

    2024年02月15日
    浏览(31)
  • F. Editorial for Two(二分答案+反悔贪心)

    F. Editorial for Two 给定一个 n n n 和 k k k ,以及一个长度为 n n n 数组。现在从 n n n 个数中,挑出 k k k 个数,称作个子序列。然后将这个子序列分成两部分,记作子序列1和子序列2。那么子序列1和子序列2都有一个对应的和。这两个和能够比较出一个最大值。现在我们要求的是

    2024年02月08日
    浏览(27)
  • 【每日一题Day224】LC2517礼盒的最大甜蜜度 | 二分答案

    礼盒的最大甜蜜度【LC2517】 You are given an array of positive integers price where price[i] denotes the price of the ith candy and a positive integer k . The store sells baskets of k distinct candies. The tastiness of a candy basket is the smallest absolute difference of the prices of any two candies in the basket. Return the maximum tastiness of a

    2024年02月07日
    浏览(29)
  • 【洛谷 P1024】[NOIP2001 提高组] 一元三次方程求解 题解(数学+二分答案)

    有形如: a x 3 + b x 2 + c x + d = 0 a x^3 + b x^2 + c x + d = 0 a x 3 + b x 2 + c x + d = 0 这样的一个一元三次方程。给出该方程中各项的系数( a , b , c , d a,b,c,d a , b , c , d 均为实数),并约定该方程存在三个不同实根(根的范围在 − 100 -100 − 100 至 100 100 100 之间),且根与根之差的绝对值

    2024年02月06日
    浏览(25)
  • # - LeetCode 704-二分查找 |LeetCode 27-移除元素

    ##  LeetCode 704-二分查找 -题目描述:给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target , -写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1。 给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target  , 写一个函数搜索 nums

    2024年02月16日
    浏览(43)
  • leetcode 二分查找小结

    原始思路: 但是,挪一挪的步骤最差的时候时间复杂度也能达到O(n),所以另一种避免这种情况的思路是我们分别使用二分查找去寻找区间的最左和最右。 上面的寻找target的代码(while …)无法精确地找到最左,因此我们需要对其进行一些改写。关键是要在找到一个值的时候不

    2024年02月08日
    浏览(43)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包