• 1385. Find the Distance Value Between Two Arrays


    Given two integer arrays arr1 and arr2, and the integer dreturn 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.

    可以n2 2层for循环求一边

    也可以nlogn,排序arr2,然后对于每个arr1的元素去arr2里二分搜索arr1元素应该在的位置,然后和前后两个值做差对比下

    还有一个nlogn的方法,对arr1和arr2排序,然后搞两个指针i和j去搞.

    class Solution(object):
        def findTheDistanceValue(self, arr1, arr2, d):
            """
            :type arr1: List[int]
            :type arr2: List[int]
            :type d: int
            :rtype: int
            """
            arr2 = sorted(arr2)
            ans = 0
            for value in arr1:
                l = 0
                r = len(arr2) - 1
                while l <= r:
                    mid = (l + r) / 2
                    if arr2[mid] <= value:
                        l = mid + 1
                    else:
                        r = mid - 1
                ok = True
                if l - 1 >= 0:
                    ok = abs(arr2[l - 1] - value) > d
                if l < len(arr2) and ok:
                    ok = abs(arr2[l] - value) > d
                if ok:
                    ans += 1
            return ans
  • 相关阅读:
    js38---门面模式
    js37---Function.prototype
    js36---函数嵌套
    js35
    js34
    js33--责任链模式
    js32---CommonUtil.js
    龙芯服务器参数
    SQLSERVER 秘钥整理
    IOMETER的简单使用
  • 原文地址:https://www.cnblogs.com/whatyouthink/p/13208217.html
Copyright © 2020-2023  润新知