当前位置:网站首页>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

边栏推荐
- Discourse 自定义头部链接(Custom Header Links)
- Is interprofessional examination difficult?Low success rate of "going ashore"?Please accept this practical guide!
- 刚出道“一战成名”,安全、舒适一个不落
- 10. Redis implements likes (Set) and obtains the total number of likes
- VS QT——ui不显示新添加成员(控件)||代码无提示
- [Godot][GDScript] 二维洞穴地图随机生成
- 6. Display comments and replies
- Software accumulation -- Screenshot software ScreenToGif
- SQL injection Less47 (error injection) and Less49 (time blind injection)
- 字体压缩神器font-spider的使用
猜你喜欢

【异常】The field file exceeds its maximum permitted size of 1048576 bytes.

LeetCode中等题之分数加减运算

LeetCode简单题之找到和最大的长度为 K 的子序列

Classic linked list OJ strong training problem - fast and slow double pointer efficient solution

分布式系统架构需要解决的问题

【C语言】进制转换一般方法

12 Disk related commands

YOLOV5学习笔记(二)——环境安装+运行+训练

CorelDRAW2022精简亚太新增功能详细介绍

STM32问题合集
随机推荐
CorelDRAW2022精简亚太新增功能详细介绍
5. SAP ABAP OData 服务如何支持 $filter (过滤)操作
Basic learning about Redis related content
SQL injection Less54 (limited number of SQL injection + union injection)
Ambiguous method call.both
遗留系统的自动化策略
Detailed explanation of TCP (3)
品牌广告投放平台的中台化应用与实践
冒泡排序、选择排序、直接插入排序、二分法查找
10. Redis implements likes (Set) and obtains the total number of likes
CentOS7下mysql5.7.37的卸载【完美方案】
公司官网建站笔记(六):域名进行公安备案并将备案号显示在网页底部
YOLOV5学习笔记(二)——环境安装+运行+训练
【C语言】表达式求值的一般方法
Is interprofessional examination difficult?Low success rate of "going ashore"?Please accept this practical guide!
mycat的主从关系 垂直分库 水平分表 以及mycat分片联表查询的配置详解(mysql5.7系列)
局域网电脑硬件信息收集工具
[Godot][GDScript] 二维洞穴地图随机生成
递归查询单表-单表树结构-(自用)
String为什么不可变?