-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
containsNearbyAlmostDuplicate.cpp
36 lines (25 loc) · 1.07 KB
/
containsNearbyAlmostDuplicate.cpp
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
class Solution {
public:
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
int n = nums.size();
if(n == 0 || k < 0 || t < 0) return false;
unordered_map<int, int> buckets;
for(int i = 0; i < n; ++i) {
int num = nums[i];
int bucket = num/ ((long) t + 1);
if(num < 0) --bucket;
if(buckets.find(bucket) != buckets.end()) return true;
buckets[bucket] = num;
if(buckets.find(bucket - 1) != buckets.end() && (long) num - buckets[bucket - 1] <= t)
return true;
if(buckets.find(bucket + 1) != buckets.end() && (long) buckets[bucket + 1] - num <= t)
return true;
if(buckets.size() > k) {
int key_to_remove = nums[i-k] / ((long)t + 1);
if(nums[i-k] < 0) --key_to_remove;
buckets.erase(key_to_remove);
}
}
return false;
}
};