-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathcontains-duplicate-iii.cpp
More file actions
36 lines (31 loc) · 1.01 KB
/
contains-duplicate-iii.cpp
File metadata and controls
36 lines (31 loc) · 1.01 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
class Solution {
public:
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
if (k <= 0) {
return false;
}
//we need a data structure that inserts/deletes easily
//and can locate a specific element easily
//a balanced binary tree will be appropriate
multiset<int64_t> bbt;
int64_t i64t = t;
for (size_t i = 0; i < nums.size(); ++i) {
if (bbt.size() > k) {
bbt.erase(bbt.find(nums[i-k-1]));
}
auto iter = bbt.lower_bound(nums[i]);
auto before = iter;
if (before != bbt.begin() && !bbt.empty()) {
--before;
}
if (iter != bbt.end() && abs(nums[i] - *iter) <= i64t) {
return true;
} else if (before != bbt.end() && abs(nums[i] - *before) <= i64t) {
return true;
} else {
bbt.insert(nums[i]);
}
}
return false;
}
};