当前位置:网站首页>The distance value between two arrays of LeetCode simple questions
The distance value between two arrays of LeetCode simple questions
2022-07-31 03:02:00 【·The sea of stars】
题目
给你两个整数数组 arr1 , arr2 和一个整数 d ,请你返回两个数组之间的 距离值 .
「距离值」 定义为符合此距离要求的元素数目:对于元素 arr1[i] ,不存在任何元素 arr2[j] 满足 |arr1[i]-arr2[j]| <= d .
示例 1:
输入:arr1 = [4,5,8], arr2 = [10,9,1,8], d = 2
输出:2
解释:
对于 arr1[0]=4 我们有:
|4-10|=6 > d=2
|4-9|=5 > d=2
|4-1|=3 > d=2
|4-8|=4 > d=2
所以 arr1[0]=4 符合距离要求
对于 arr1[1]=5 我们有:
|5-10|=5 > d=2
|5-9|=4 > d=2
|5-1|=4 > d=2
|5-8|=3 > d=2
所以 arr1[1]=5 也符合距离要求
对于 arr1[2]=8 我们有:
|8-10|=2 <= d=2
|8-9|=1 <= d=2
|8-1|=7 > d=2
|8-8|=0 <= d=2
存在距离小于等于 2 的情况,不符合距离要求
故而只有 arr1[0]=4 和 arr1[1]=5 两个符合距离要求,距离值为 2
示例 2:
输入:arr1 = [1,4,2,3], arr2 = [-4,-3,6,10,20,30], d = 3
输出:2
示例 3:
输入:arr1 = [2,1,100,3], arr2 = [-5,-2,10,-3,7], d = 6
输出:1
提示:
1 <= arr1.length, arr2.length <= 500
-10^ 3 <= arr1[i], arr2[j] <= 10^3
0 <= d <= 100
来源:力扣(LeetCode)
解题思路
根据题目的要求可知,数组1The elements in are and arrays respectively2Find the absolute distance of the elements in ,如果数组1an element in an array2All elements of the distance meet the requirements,Then it can be counted as 1.A simple idea here is to convert arrays2排序,then look at the array1The elements in should be in the array2中的位置,Then probe the distance between the two elements before and after it,If these two distances meet the requirements, then the remaining elements naturally meet the requirements.
class Solution:
def findTheDistanceValue(self, arr1: List[int], arr2: List[int], d: int) -> int:
arr2.sort()
count=0
for i in arr1:
index=bisect.bisect(arr2,i)
if index==0:
if abs(i-arr2[index])>d:
count+=1
elif index==len(arr2):
if abs(i-arr2[index-1])>d:
count+=1
else:
if abs(i-arr2[index-1])>d and abs(i-arr2[index])>d:
count+=1
return count

边栏推荐
- SQL注入 Less54(限制次数的SQL注入+union注入)
- Mathematics to solve the problem - circular linked list
- Word/Excel fixed table size, when filling in the content, the table does not change with the cell content
- 7、私信列表
- mycat的主从关系 垂直分库 水平分表 以及mycat分片联表查询的配置详解(mysql5.7系列)
- JetPack组件Databinding
- TCP/IP four-layer model
- Mycat's master-slave relationship, vertical sub-database, horizontal sub-table, and detailed configuration of mycat fragmented table query (mysql5.7 series)
- Project (5) - Small target detection tph-yolov5
- Go 项目实战-获取多级分类下的全部商品
猜你喜欢
随机推荐
【C语言】预处理操作
print task sorting js od huawei
SonarQube的BUG定义
SQL injection Less47 (error injection) and Less49 (time blind injection)
【编译原理】递归下降语法分析设计原理与实现
Getting Started with CefSharp - winform
10. Redis implements likes (Set) and obtains the total number of likes
Unity3D Button 鼠标悬浮进入与鼠标悬浮退出按钮事件
TCP详解(二)
PMP微信群日常习题
CentOS7下mysql5.7.37的卸载【完美方案】
分布式系统架构需要解决的问题
【C语言】求两个整数m和n的最大公因数和最小公倍数之和一般方法,经典解法
JetPack component Databinding
Moxa NPort 设备缺陷可能使关键基础设施遭受破坏性攻击
Compile Hudi
MP使用时的几个常见报错
The use of font compression artifact font-spider
JS 函数 this上下文 运行时点语法 圆括号 数组 IIFE 定时器 延时器 self.备份上下文 call apply
The modification is not properly placed in the sandbox, causing Apple compatibility issues









