-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path220.cpp
More file actions
24 lines (24 loc) · 782 Bytes
/
Copy path220.cpp
File metadata and controls
24 lines (24 loc) · 782 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
class Solution {
// Runtime: 20 ms, faster than 71.64% of C++ online submissions for Contains
// Duplicate III. Memory Usage: 10.9 MB, less than 32.96% of C++ online
// submissions for Contains Duplicate III.
public:
bool containsNearbyAlmostDuplicate(vector<int> &nums, int k, int t) {
if (k < 1 or t < 0)
return false;
set<int> s;
for (int i = 0; i < nums.size(); ++i) {
if (i > k)
s.erase(nums[i - k - 1]);
auto [it, success] = s.insert(nums[i]);
if (not success)
return true;
if (it != begin(s) and abs((long long)*prev(it) - (long long)*it) <= t)
return true;
if (next(it) != end(s) and
abs((long long)*next(it) - (long long)*it) <= t)
return true;
}
return false;
}
};