国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

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

這篇具有很好參考價(jià)值的文章主要介紹了LeetCode719. Find K-th Smallest Pair Distance——二分答案。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

一、題目

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到最大差值之間,二分判斷文章來源地址http://www.zghlxwxcb.cn/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的數(shù)對(duì)個(gè)數(shù)
    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;
    }
};

到了這里,關(guān)于LeetCode719. Find K-th Smallest Pair Distance——二分答案的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

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

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

    本文屬于「征服LeetCode」系列文章之一,這一系列正式開始于2021/08/12。由于LeetCode上部分題目有鎖,本系列將至少持續(xù)到刷完所有無鎖題之日為止;由于LeetCode還在不斷地創(chuàng)建新題,本系列的終止日期可能是永遠(yuǎn)。在這一系列刷題文章中,我不僅會(huì)講解多種解題思路及其優(yōu)化,

    2024年02月09日
    瀏覽(24)
  • C++二分算法(二分查找&二分答案)細(xì)節(jié)詳解

    ?二分算法可以分為 二分查找 和 二分答案 。 以在一個(gè) 升序數(shù)組 中查找一個(gè)數(shù)為例。它每次考察數(shù)組當(dāng)前部分的 中間元素 ,如果中間元素剛好是要找的,就結(jié)束搜索過程;如果中間元素小于所查找的值,那么左側(cè)的只會(huì)更小,不會(huì)有所查找的元素,只需到右側(cè)查找;如果

    2024年02月08日
    瀏覽(19)
  • LeetCode646. Maximum Length of Pair Chain——?jiǎng)討B(tài)規(guī)劃

    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日
    瀏覽(18)
  • Python數(shù)據(jù)結(jié)構(gòu)與算法篇(五)-- 二分查找與二分答案

    Python數(shù)據(jù)結(jié)構(gòu)與算法篇(五)-- 二分查找與二分答案

    1.1 定義 ????????二分查找又稱折半查找、二分搜索、折半搜索等,是一種在靜態(tài)查找表中查找特定元素的算法。 ????????所謂靜態(tài)查找表,即只能對(duì)表內(nèi)的元素做查找和讀取操作,不允許插入或刪除元素。 ????????使用二分查找算法,必須保證查找表中存放的是有

    2023年04月20日
    瀏覽(25)
  • 【二分答案】CF1661 C

    【二分答案】CF1661 C

    Problem - C - Codeforces 題意: 思路: 在check的時(shí)候,我們要盡量用算貢獻(xiàn)的思想,并且大膽貪心 Code:

    2024年02月15日
    瀏覽(21)
  • F. Editorial for Two(二分答案+反悔貪心)

    F. Editorial for Two(二分答案+反悔貪心)

    F. Editorial for Two 給定一個(gè) n n n 和 k k k ,以及一個(gè)長(zhǎng)度為 n n n 數(shù)組。現(xiàn)在從 n n n 個(gè)數(shù)中,挑出 k k k 個(gè)數(shù),稱作個(gè)子序列。然后將這個(gè)子序列分成兩部分,記作子序列1和子序列2。那么子序列1和子序列2都有一個(gè)對(duì)應(yīng)的和。這兩個(gè)和能夠比較出一個(gè)最大值?,F(xiàn)在我們要求的是

    2024年02月08日
    瀏覽(21)
  • 【每日一題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日
    瀏覽(19)
  • 【洛谷 P1024】[NOIP2001 提高組] 一元三次方程求解 題解(數(shù)學(xué)+二分答案)

    有形如: 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 這樣的一個(gè)一元三次方程。給出該方程中各項(xiàng)的系數(shù)( a , b , c , d a,b,c,d a , b , c , d 均為實(shí)數(shù)),并約定該方程存在三個(gè)不同實(shí)根(根的范圍在 ? 100 -100 ? 100 至 100 100 100 之間),且根與根之差的絕對(duì)值

    2024年02月06日
    瀏覽(18)
  • # - LeetCode 704-二分查找 |LeetCode 27-移除元素

    ## ?LeetCode 704-二分查找 -題目描述:給定一個(gè) n 個(gè)元素有序的(升序)整型數(shù)組 nums 和一個(gè)目標(biāo)值 target , -寫一個(gè)函數(shù)搜索 nums 中的 target,如果目標(biāo)值存在返回下標(biāo),否則返回 -1。 給定一個(gè) n 個(gè)元素有序的(升序)整型數(shù)組 nums 和一個(gè)目標(biāo)值 target ?, 寫一個(gè)函數(shù)搜索 nums

    2024年02月16日
    瀏覽(29)
  • leetcode 二分查找小結(jié)

    leetcode 二分查找小結(jié)

    原始思路: 但是,挪一挪的步驟最差的時(shí)候時(shí)間復(fù)雜度也能達(dá)到O(n),所以另一種避免這種情況的思路是我們分別使用二分查找去尋找區(qū)間的最左和最右。 上面的尋找target的代碼(while …)無法精確地找到最左,因此我們需要對(duì)其進(jìn)行一些改寫。關(guān)鍵是要在找到一個(gè)值的時(shí)候不

    2024年02月08日
    瀏覽(21)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包