Skip to content

Commit cd52067

Browse files
committed
feat(data structures, arrays): contains duplicates ii
1 parent 5493cec commit cd52067

16 files changed

+147
-35
lines changed

datastructures/arrays/contains_duplicates/README.md

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,18 @@ Example 3:
1717
Input: nums = [1,1,1,3,3,4,3,2,4,2]
1818
Output: true
1919

20+
---
21+
2022
## Contains Duplicates II
2123

22-
Given an integer array nums and an integer k, return true if there are two distinct indices i and j in the array such that nums[i] == nums[j] and abs(i - j) <= k.
24+
Given an integer array nums and an integer k, return true if there are two distinct indices i and j in the array such
25+
that nums[i] == nums[j] and abs(i - j) <= k.
26+
27+
Constrains:
28+
29+
- 1 <= nums.length <= 10^3
30+
- -10^3 <= nums[i] <= 10^3
31+
- 0 <= k <= 10^4
2332

2433
Example 1:
2534

@@ -34,6 +43,53 @@ Example 3:
3443
Input: nums = [1,2,3,1,2,3], k = 2
3544
Output: false
3645

46+
### Solution
47+
48+
The core intuition of solving this problem is maintaining a sliding window of size k to track elements within a limited
49+
range using a set. As we iterate through the array, we check if the current element already exists in the set,
50+
indicating a duplicate within the range. If it exists, we return TRUE. Otherwise, the element is added to the set.
51+
If the set size exceeds k, we remove the oldest element to ensure that the set only contains elements within the valid
52+
range at any time.
53+
54+
Using the above intuition, the solution can be implemented as follows:
55+
56+
1. Create a set, `seen`, to track elements within the sliding window of size `k`.
57+
2. Loop through each index `i` of the array `nums`.
58+
- If the current element, `nums[i]`, already exists in the set, a duplicate exists within a range of `k` indices.
59+
Therefore, we return TRUE.
60+
- Add the current element to the set.
61+
- If the set’s size exceeds `k`, remove the oldest element in the window (`nums[i - k]`) to maintain the window’s size.
62+
This ensures only elements within the range k are tracked.
63+
64+
3. If the loop completes without finding duplicates, we return FALSE.
65+
66+
Let’s look at the following illustration to get a better understanding of the solution:
67+
68+
![Solution 1](./images/solutions/contains_duplicates_ii_solution_1.png)
69+
![Solution 2](./images/solutions/contains_duplicates_ii_solution_2.png)
70+
![Solution 3](./images/solutions/contains_duplicates_ii_solution_3.png)
71+
![Solution 4](./images/solutions/contains_duplicates_ii_solution_4.png)
72+
![Solution 5](./images/solutions/contains_duplicates_ii_solution_5.png)
73+
![Solution 6](./images/solutions/contains_duplicates_ii_solution_6.png)
74+
![Solution 7](./images/solutions/contains_duplicates_ii_solution_7.png)
75+
![Solution 8](./images/solutions/contains_duplicates_ii_solution_8.png)
76+
![Solution 9](./images/solutions/contains_duplicates_ii_solution_9.png)
77+
![Solution 10](./images/solutions/contains_duplicates_ii_solution_10.png)
78+
![Solution 11](./images/solutions/contains_duplicates_ii_solution_11.png)
79+
80+
#### Time Complexity
81+
82+
The time complexity of the solution is O(n), where n is the length of the input array `nums`.
83+
This is because we iterate through the array once, performing constant-time operations for each element.
84+
85+
#### Space Complexity
86+
87+
The space complexity of the solution is O(min(n, k)), where n is the length of the input array `nums` and k is the
88+
maximum number of steps between duplicate elements. This is because we use a set to store the elements within the
89+
sliding window of size `k`, and the maximum size of the set is limited by the minimum of `n` and `k`.
90+
91+
---
92+
3793
## Contains Duplicate III
3894

3995
Given an integer array nums and two integers k and t, return true if there are two distinct indices i and j in the array

datastructures/arrays/contains_duplicates/__init__.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,60 @@
11
import sys
2-
from typing import List
2+
from typing import List, Set
33

44

55
def contains_nearby_duplicate(nums: List[int], k: int) -> bool:
6+
"""
7+
Checks if there are any duplicate elements within k steps of each other
8+
in the given list of numbers.
9+
10+
Args:
11+
nums (List[int]): The list of numbers to check.
12+
k (int): The maximum number of steps between duplicate elements.
13+
14+
Returns:
15+
bool: True if there are any duplicate elements within k steps of each other, False otherwise.
16+
"""
17+
# Dictionary to store the indices of the numbers we have seen so far
618
d = dict()
719
for i, n in enumerate(nums):
20+
# If we have seen this number before and it is within k steps of the current position
821
if n in d and i - d[n] <= k:
922
return True
23+
# Store the index of the current number
1024
d[n] = i
25+
# If we have not found any duplicate elements within k steps of each other
26+
return False
27+
28+
29+
def contains_nearby_duplicates_2(nums: List[int], k: int) -> bool:
30+
"""
31+
Checks if there are any duplicate elements within k steps of each other
32+
in the given list of numbers.
33+
34+
Args:
35+
nums (List[int]): The list of numbers to check.
36+
k (int): The maximum number of steps between duplicate elements.
37+
38+
Returns:
39+
bool: True if there are any duplicate elements within k steps of each other, False otherwise.
40+
"""
41+
# Set to store the numbers we have seen so far
42+
seen: Set[int] = set()
43+
# Iterate over the list of numbers
44+
for idx in range(len(nums)):
45+
# If we have seen this number before
46+
if nums[idx] in seen:
47+
# Return True
48+
return True
49+
# Add the current number to the set of seen numbers
50+
seen.add(nums[idx])
51+
52+
# If we have seen more than k numbers
53+
if len(seen) > k:
54+
# Remove the number that is k steps behind the current number
55+
seen.remove(nums[idx - k])
56+
57+
# If we have not found any duplicate elements within k steps of each other
1158
return False
1259

1360

28.6 KB
Loading
28.2 KB
Loading
28.8 KB
Loading
21 KB
Loading
19.8 KB
Loading
21 KB
Loading
19.5 KB
Loading
26.6 KB
Loading

0 commit comments

Comments
 (0)