LeetCode #1385. Find the Distance Value Between Two Arrays
题目
1385. Find the Distance Value Between Two Arrays
解题方法
遍历arr1,再以其中的每个数字遍历arr2,判断arr2中的数到这个数字的距离是否都是大于d的,如果是就给distance value + 1,否则break。
时间复杂度:O(mn), m = len(arr1), n = len(arr2)
空间复杂度:O(1)
代码
class Solution:
def findTheDistanceValue(self, arr1: List[int], arr2: List[int], d: int) -> int:
dv = 0
for i in arr1:
for j in arr2:
if abs(i-j) <= d:
break
else:
dv += 1
return dv

浙公网安备 33010602011771号