Skip to content

Commit ed12c69

Browse files
committed
Solution added.
1 parent 8100caf commit ed12c69

File tree

1 file changed

+27
-0
lines changed
  • 30 Days September Challange/Week 1/2. Contains Duplicate III

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
"""
2+
Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute difference between i and j is at most k.
3+
4+
Example 1:
5+
6+
Input: nums = [1,2,3,1], k = 3, t = 0
7+
Output: true
8+
Example 2:
9+
10+
Input: nums = [1,0,1,1], k = 1, t = 2
11+
Output: true
12+
Example 3:
13+
14+
Input: nums = [1,5,9,1,5,9], k = 2, t = 3
15+
Output: false
16+
"""
17+
18+
19+
class Solution:
20+
def containsNearbyAlmostDuplicate(self, nums: List[int], k: int, t: int) -> bool:
21+
if t == 0 and len(set(nums)) == len(nums):
22+
return False
23+
for i, num in enumerate(nums):
24+
for j in range(i + 1, len(nums)):
25+
if abs(num - nums[j]) <= t and abs(i - j) <= k:
26+
return True
27+
return False

0 commit comments

Comments
 (0)