-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathContainsDuplicateIII_v1.py
More file actions
39 lines (29 loc) · 1.21 KB
/
ContainsDuplicateIII_v1.py
File metadata and controls
39 lines (29 loc) · 1.21 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
39
#!/usr/bin/env python
# encoding: utf-8
from collections import OrderedDict
class Solution:
# @param {integer[]} nums
# @param {integer} k
# @param {integer} t
# @return {boolean}
def containsNearbyAlmostDuplicate(self, nums, k, t):
if not k:
return False
d = OrderedDict()
for n in nums:
key = n if t == 0 else n // t # Hash n to key, if n' has the same key, diff between n' and n won't be greater than t, for key+1, key-1, it might be. So key+1, key-1 need to check
for m in (d.get(key-1), d.get(key), d.get(key+1)):
if m is not None and abs(m - n) <= t:
return True
if len(d) == k:
d.popitem(False) # Remove head of OrderedDict
d[key] = n
return False
if __name__ == '__main__':
s = Solution()
print s.containsNearbyAlmostDuplicate([1,5,8,9,16], 1, 1)
print s.containsNearbyAlmostDuplicate([1,5,8,16], 1, 3)
print s.containsNearbyAlmostDuplicate([0,2147483647], 1, 2147483647)
print s.containsNearbyAlmostDuplicate([-3,3], 2, 4)
print s.containsNearbyAlmostDuplicate([4,2], 2, 1)
print s.containsNearbyAlmostDuplicate([0], 0, 0)