-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path220.cpp
More file actions
25 lines (24 loc) · 758 Bytes
/
220.cpp
File metadata and controls
25 lines (24 loc) · 758 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
25
class Solution {
public:
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
multiset<long long> st;
int n = nums.size();
int initLength = min(n, k + 1);
for (int i = 0; i < n; ++i) {
if (i > k) {
st.erase(st.find(nums[i - k - 1]));
}
int current = nums[i];
auto itLower = st.lower_bound(current);
if (itLower != st.end()) {
if (abs(*itLower - current) <= t) return true;
}
if (itLower != st.begin()) {
itLower--;
if (abs(*itLower - current) <= t) return true;
}
st.insert(current);
}
return false;
}
};