-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontainsDuplicate3.py
More file actions
24 lines (19 loc) · 885 Bytes
/
containsDuplicate3.py
File metadata and controls
24 lines (19 loc) · 885 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# problem: https://leetcode.com/problems/contains-duplicate-iii/
# Runtime: 1004 ms, faster than 6.11% of Python3 online submissions for Contains Duplicate III.
# Memory Usage: 17.2 MB, less than 89.59% of Python3 online submissions for Contains Duplicate III.
from typing import List
class Solution:
def containsNearbyAlmostDuplicate(self, nums: List[int], k: int, t: int) -> bool:
from sortedcontainers import SortedList
window = SortedList()
for i, val in enumerate(nums):
if i - k > 0:
idx = window.index(nums[i - k - 1])
window.pop(idx)
window.add(val)
j = window.index(val)
if j - 1 >= 0 and abs(window[j-1] - val) <= t:
return True
if j + 1 < len(window) and abs(window[j+1] - val) <= t:
return True
return False