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

LeetCode 1385. Find the Distance Value Between Two Arrays

這篇具有很好參考價值的文章主要介紹了LeetCode 1385. Find the Distance Value Between Two Arrays。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報違法"按鈕提交疑問。

Given two integer arrays?arr1?and?arr2, and the integer?d,?return the distance value between the two arrays.

The distance value is defined as the number of elements?arr1[i]?such that there is not any element?arr2[j]?where?|arr1[i]-arr2[j]| <= d.

Example 1:

Input: arr1 = [4,5,8], arr2 = [10,9,1,8], d = 2
Output: 2
Explanation: 
For arr1[0]=4 we have: 
|4-10|=6 > d=2 
|4-9|=5 > d=2 
|4-1|=3 > d=2 
|4-8|=4 > d=2 
For arr1[1]=5 we have: 
|5-10|=5 > d=2 
|5-9|=4 > d=2 
|5-1|=4 > d=2 
|5-8|=3 > d=2
For arr1[2]=8 we have:
|8-10|=2 <= d=2
|8-9|=1 <= d=2
|8-1|=7 > d=2
|8-8|=0 <= d=2

Example 2:

Input: arr1 = [1,4,2,3], arr2 = [-4,-3,6,10,20,30], d = 3
Output: 2

Example 3:

Input: arr1 = [2,1,100,3], arr2 = [-5,-2,10,-3,7], d = 6
Output: 1

Constraints:

  • 1 <= arr1.length, arr2.length <= 500
  • -1000 <= arr1[i], arr2[j] <= 1000
  • 0 <= d <= 100

這題第一反應(yīng)就是讀不懂題,第二反應(yīng)就是不會做,嗯,看了答案確實(shí)如同大家所說的這道題的表述絕對有點(diǎn)問題,太繞了!已經(jīng)不想解釋這道題到底想讓我干嘛了畢竟現(xiàn)在腦子已經(jīng)不清醒了……大概就是要在另一個數(shù)組里找對于某個數(shù)字來說,是不是所有數(shù)字和這個數(shù)字的絕對值之差都大于一個數(shù)。

于是就可以轉(zhuǎn)換成二分查找的思想,找到在這個數(shù)組里這個數(shù)字應(yīng)該插在哪兒,并比較離他最近的兩個數(shù)字和它的絕對值之差看是不是都大于d,如果都大于那就符合條件。

于是就艱難地寫了個二分,歷盡千辛萬苦憑借一己之力和一些些看小筆記發(fā)現(xiàn)愚蠢錯誤,最后寫出來了,啊。幾個愚蠢錯誤:

1. end = arr.length - 1忘了- 1

2. num < arr[mid] 忘了arr[]

3. 這道題繞來繞去的大于小于也傻傻分不清

對于二分插入的思想我寫在二分大總結(jié)里了:LeetCode 704. Binary Search_wenyq7的博客-CSDN博客

精華思想就在于,還是用low <= high的判斷條件,最后的順序是high, num, low。

class Solution {
    public int findTheDistanceValue(int[] arr1, int[] arr2, int d) {
        int result = 0;
        Arrays.sort(arr2);

        for (int i : arr1) {
            if (allLarger(i, arr2, d)) {
                result++;
            }
        }
        return result;
    }

    private boolean allLarger(int num, int[] arr, int d) {
        // find the position i for num to insert into arr
        // then check if the diff between (arr[i - 1] and arr[i + 1]) and num > d
        int start = 0;
        int end = arr.length - 1;

        while (start <= end) {
            int mid = start + (end - start) / 2;
            if (num > arr[mid]) {
                start = mid + 1;
            } else if (num < arr[mid]) {
                end = mid - 1;
            } else {
                return 0 > d;
            }
        }
        // end start
        if (end < 0) {
            return arr[0] - num > d;
        }
        if (start > arr.length - 1) {
            return num - arr[arr.length - 1] > d;
        }
        return (num - arr[end] > d) && (arr[start] - num > d);
    }
}

以及看到有人直接用了TreeSet,機(jī)智,只要記得還有這個數(shù)據(jù)結(jié)構(gòu)能用就行,真懶得花時間了:LeetCode - The World's Leading Online Programming Learning Platform文章來源地址http://www.zghlxwxcb.cn/news/detail-688079.html

到了這里,關(guān)于LeetCode 1385. Find the Distance Value Between Two Arrays的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

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

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

相關(guān)文章

  • 【Spark】What is the difference between Input and Shuffle Read

    Spark調(diào)參過程中 保持每個task的 input + shuffle read 量在300-500M左右比較合適 The Spark UI is documented here: https://spark.apache.org/docs/3.0.1/web-ui.html The relevant paragraph reads: Input: Bytes read from storage in this stage Output: Bytes written in storage in this stage Shuffle read: Total shuffle bytes and records read, includes b

    2024年02月06日
    瀏覽(19)
  • Pytorch unsupported Microsoft Visual Studio version! Only the versions between 2017 and 2019

    Windows 下 Pytorch需要編譯cpp文件,出現(xiàn)如下錯誤: fatal error C1189: #error: ?-- unsupported Microsoft Visual Studio version! Only the versions between 2017 and 2019 (inclusive) are supported! The nvcc flag \\\'-allow-unsupported-compiler\\\' can be used to override this version check 我安裝的VS2022,那么需要重新安裝VS2019么? 其實(shí)不

    2024年02月11日
    瀏覽(19)
  • LeetCode 1. Two Sum 兩數(shù)之和

    題目描述 給定一個整數(shù)數(shù)組 nums?和一個目標(biāo)值 target,請你在該數(shù)組中找出和為目標(biāo)值的那?兩個?整數(shù),并返回他們的數(shù)組下標(biāo)。 你可以假設(shè)每種輸入只會對應(yīng)一個答案。但是,數(shù)組中同一個元素不能使用兩遍。 示例: 給定 nums = [2, 7, 11, 15], target = 9 因?yàn)?nums[0] + nums[1] = 2

    2023年04月25日
    瀏覽(24)
  • leetcode Median of Two Sorted Arrays

    Given two sorted arrays? nums1 ?and? nums2 ?of size? m ?and? n ?respectively, return? the median ?of the two sorted arrays. The overall run time complexity should be? O(log (m+n)) . Example 1: Example 2: Constraints: nums1.length == m nums2.length == n 0 = m = 1000 0 = n = 1000 1 = m + n = 2000 -106 = nums1[i], nums2[i] = 106

    2023年04月08日
    瀏覽(25)
  • leetcode 445. Add Two Numbers II(兩數(shù)相加)

    leetcode 445. Add Two Numbers II(兩數(shù)相加)

    用鏈表代表2個數(shù)字,這2個數(shù)字相加的和用鏈表返回。 最高位在鏈表的head. 思路: 1.鏈表逆序 數(shù)字相加是從低位到高位的,然而鏈表中的數(shù)字是從高位指向低位。 所以涉及到鏈表的逆序。 逆序之后只需從head到tail把兩個鏈表的數(shù)字相加,再用一個int表示進(jìn)位。 鏈表的逆序

    2024年02月16日
    瀏覽(26)
  • LeetCode //C - 4. Median of Two Sorted Arrays

    Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)) . ? Example 1: Input: nums1 = [1,3], nums2 = [2] Output: 2.00000 Explanation: merged array = [1,2,3] and median is 2. Example 2: Input: nums1 = [1,2], nums2 = [3,4] Output: 2.50000 Explanati

    2024年02月06日
    瀏覽(25)
  • leetcode 2446. Determine if Two Events Have Conflict

    You are given two arrays of strings that represent two inclusive events that happened on the same day, event1 and event2, where: event1 = [startTime1, endTime1] and event2 = [startTime2, endTime2]. Event times are valid 24 hours format in the form of HH:MM. A conflict happens when two events have some non-empty intersection (i.e., some moment is common to bo

    2024年02月22日
    瀏覽(24)
  • LeetCode //167. Two Sum II - Input Array Is Sorted

    Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1 = index1 index2 numbers.length. Return the indices of the two numbers, index1 and index2 , added by one as an integer array [index1

    2024年02月15日
    瀏覽(28)
  • LeetCode //2723. Add Two Promises (Day 30 of LC JavaScript Challenage)

    Given two promises promise1 and promise2 , return a new promise. promise1 and promise2 will both resolve with a number. The returned promise should resolve with the sum of the two numbers. ? Example 1: Input: promise1 = new Promise(resolve = setTimeout(() = resolve(2), 20)), promise2 = new Promise(resolve = setTimeout(() = resolve(5), 60)) Output: 7 Explana

    2024年02月11日
    瀏覽(23)
  • LeetCode --- 1880. Check if Word Equals Summation of Two Words 解題報告

    The? letter value ?of a letter is its position in the alphabet? starting from 0 ?(i.e.? \\\'a\\\' - 0 ,? \\\'b\\\' - 1 ,? \\\'c\\\' - 2 , etc.). The? numerical value ?of some string of lowercase English letters? s ?is the? concatenation ?of the? letter values ?of each letter in? s , which is then? converted ?into an integer. For example, if? s = \\\"acb\\\" , we

    2024年02月13日
    瀏覽(29)

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

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

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

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包