LeetCode 1385. Find the Distance Value Between Two Arrays

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

这题第一反应就是读不懂题,第二反应就是不会做,嗯,看了答案确实如同大家所说的这道题的表述绝对有点问题,太绕了!已经不想解释这道题到底想让我干嘛了毕竟现在脑子已经不清醒了......大概就是要在另一个数组里找对于某个数字来说,是不是所有数字和这个数字的绝对值之差都大于一个数。

于是就可以转换成二分查找的思想,找到在这个数组里这个数字应该插在哪儿,并比较离他最近的两个数字和它的绝对值之差看是不是都大于d,如果都大于那就符合条件。

于是就艰难地写了个二分,历尽千辛万苦凭借一己之力和一些些看小笔记发现愚蠢错误,最后写出来了,啊。几个愚蠢错误:

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

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

  3. 这道题绕来绕去的大于小于也傻傻分不清

对于二分插入的思想我写在二分大总结里了: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,机智,只要记得还有这个数据结构能用就行,真懒得花时间了:LeetCode - The World's Leading Online Programming Learning Platform

相关推荐
new coder2 小时前
[算法练习]Day 7: 变长滑动窗口
数据结构·算法·leetcode
Tiny番茄9 小时前
leetcode 3. 无重复字符的最长子串
数据结构·python·算法·leetcode
Miraitowa_cheems14 小时前
LeetCode算法日记 - Day 68: 猜数字大小II、矩阵中的最长递增路径
数据结构·算法·leetcode·职场和发展·贪心算法·矩阵·深度优先
qq_5746562518 小时前
java-代码随想录第66天|Floyd 算法、A * 算法精讲 (A star算法)
java·算法·leetcode·图论
代码对我眨眼睛20 小时前
739. 每日温度 LeetCode 热题 HOT 100
算法·leetcode
zycoder.1 天前
力扣面试经典150题day3第五题(lc69),第六题(lc189)
算法·leetcode·面试
_dindong1 天前
基础算法:滑动窗口
数据结构·学习·算法·leetcode·力扣
nju_spy1 天前
力扣每日一题(二)任务安排问题 + 区间变换问题 + 排列组合数学推式子
算法·leetcode·二分查找·贪心·排列组合·容斥原理·最大堆
代码对我眨眼睛1 天前
226. 翻转二叉树 LeetCode 热题 HOT 100
算法·leetcode·职场和发展
黑色的山岗在沉睡1 天前
LeetCode 494. 目标和
算法·leetcode·职场和发展