-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontains_duplicate_iii_0220.py
More file actions
38 lines (38 loc) · 1.49 KB
/
contains_duplicate_iii_0220.py
File metadata and controls
38 lines (38 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Solution:
def containsNearbyAlmostDuplicate(
self, nums: list[int], indexDiff: int, valueDiff: int
) -> bool:
"""
Look for two different indices i and j, where:
1. |i - j| <= indexDiff (indices are close)
2. |nums[i] - nums[j]| <= valueDiff (values are close)
Args:
nums (list[int]): Input array.
indexDiff (int): Maximum distance between indices.
valueDiff (int): Maximum distance between values.
Returns:
bool: True, if a suitable pair of indices is found, otherwise False.
Time Complexity:
O(n) — each element is processed once,
checking 3 buckets = O(1).
Space Complexity:
O(min(n, indexDiff)) — we store at most indexDiff buckets.
"""
if valueDiff < 0 or indexDiff <= 0:
return False
buckets = {}
bucket_size = valueDiff + 1
for i, num in enumerate(nums):
bucket_id = num // bucket_size
if bucket_id in buckets:
return True
if (bucket_id - 1 in buckets
and abs(num - buckets[bucket_id - 1]) <= valueDiff):
return True
if (bucket_id + 1 in buckets
and abs(num - buckets[bucket_id + 1]) <= valueDiff):
return True
buckets[bucket_id] = num
if i >= indexDiff:
del buckets[nums[i - indexDiff] // bucket_size]
return False