-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathT220_contains_duplicates_3.cpp
More file actions
27 lines (25 loc) · 947 Bytes
/
T220_contains_duplicates_3.cpp
File metadata and controls
27 lines (25 loc) · 947 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
26
//My idea is to preserve a sliding window containing nearest k numbers, and check if next number collides to the numbers in the window.
class Solution {
public:
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
if (nums.size() < 2 || k == 0)
return false;
deque<int> windows_deq;
multiset<long> windows;
for (int i = 0; i < nums.size(); i++) {
if (windows.size() > k) {
int num = windows_deq.front();
windows_deq.pop_front();
windows.erase(windows.find(num));
}
auto it = windows.lower_bound((long)nums[i] - (long)t);
if (it == windows.end() || *it > (long)nums[i] + (long)t) {
// not found
windows_deq.push_back(nums[i]);
windows.insert(nums[i]);
}
else return true;
}
return false;
}
};