当前位置:网站首页>LeetCode简单题之两个数组间的距离值
LeetCode简单题之两个数组间的距离值
2022-07-31 02:56:00 【·星辰大海】
题目
给你两个整数数组 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)
解题思路
根据题目的要求可知,数组1中的元素分别和数组2中的元素求绝对距离,如果数组1中的某个元素与数组2的所有元素的距离都符合要求,那么可以计数为1。在这里一个简单的思路就是将数组2排序,然后查看数组1中的元素应该在数组2中的位置,然后探查其前后两个元素与其的距离,如果这两个距离都符合要求那么剩下的元素自然也符合要求。
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

边栏推荐
- The simulation application of common mode inductance is here, full of dry goods for everyone
- Refuse to work overtime, a productivity tool set developed by programmers
- Why is String immutable?
- The modification is not properly placed in the sandbox, causing Apple compatibility issues
- Unity3D Button mouse hover enter and mouse hover exit button events
- 19.支持向量机-优化目标和大间距直观理解
- 接口测试关键技术
- 12 磁盘相关命令
- 编译Hudi
- 公司官网建站笔记(六):域名进行公安备案并将备案号显示在网页底部
猜你喜欢

【C语言】三子棋(经典解法+一览图)

6. Display comments and replies

8. Unified exception handling (controller notifies @ControllerAdvice global configuration class, @ExceptionHandler handles exceptions uniformly)

STM32CUBEMX开发GD32F303(11)----ADC在DMA模式下扫描多个通道

YOLOV5 study notes (2) - environment installation + operation + training

SQL注入 Less54(限制次数的SQL注入+union注入)

关于 mysql8.0数据库中主键位id,使用replace插入id为0时,实际id插入后自增导致数据重复插入 的解决方法

分布式与集群是什么 ? 区别是什么?

Live Preview | KDD2022 Doctoral Dissertation Award Champion and Runner-up Dialogue

Classic linked list OJ strong training problem - fast and slow double pointer efficient solution
随机推荐
The application of AI in the whole process of medical imaging equipment
加密公司向盗窃的黑客提供报价:保留一点,把剩下的归还
print task sorting js od huawei
Modbus on AT32 MCU
YOLOV5学习笔记(二)——环境安装+运行+训练
基于opencv实现人脸检测
CentOS7下mysql5.7.37的卸载【完美方案】
【银行系列第一期】中国人民银行
Android's webview cache related knowledge collection
The principle of complete replication of virtual machines (cloud computing)
Is interprofessional examination difficult?Low success rate of "going ashore"?Please accept this practical guide!
AtCoder Beginner Contest 261 部分题解
IIR滤波器和FIR滤波器
MPPT solar charge controller data collection - through the gateway acquisition capacity battery SOC battery voltage, wi-fi
Hanyuan Hi-Tech 8-channel HDMI integrated multi-service high-definition video optical transceiver 8-channel HDMI video + 8-channel two-way audio + 8-channel 485 data + 8-channel E1 + 32-channel teleph
局域网电脑硬件信息收集工具
LeetCode 1161 The largest element in the layer and the LeetCode road of [BFS binary tree] HERODING
Installation of mysql5.7.37 under CentOS7 [perfect solution]
【C语言】进制转换一般方法
工程(五)——小目标检测tph-yolov5