-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContainsDuplicateIII.cc
More file actions
40 lines (33 loc) · 893 Bytes
/
Copy pathContainsDuplicateIII.cc
File metadata and controls
40 lines (33 loc) · 893 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <memory.h>
#include <string>
#include <sstream>
#include <iostream>
#include <vector>
#include <set>
using namespace std;
class Solution {
public:
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
if (nums.size() <= 1 || k < 1 || t < 0) return false;
multiset<long long> mset;
long long long_t = t;
for (int i = 0; i < nums.size(); ++i) {
if (mset.size() == k + 1) {
auto it = mset.find(nums[i - k - 1]);
mset.erase(it);
}
long long tmp = nums[i];
auto it = mset.lower_bound(tmp - long_t);
if (it != mset.end()) {
long long diff = *it > tmp ? *it - tmp : tmp - *it;
if (diff <= long_t) return true;
}
mset.emplace(nums[i]);
}
return false;
}
};
int main(int argc, char const* argv[]) { return 0; }