forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContains Duplicate III.cpp
44 lines (38 loc) · 1.02 KB
/
Contains Duplicate III.cpp
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
41
42
43
44
class Solution {
public:
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t)
{
if(k==0)
return false;
multiset<long> window;
for(int i=0;i<nums.size();i++)
{
if(i>k)
window.erase(nums[i-k-1]);
auto it=window.lower_bound((long)nums[i]-(long)t);
if(it!=window.end() && *it<=(long)nums[i]+(long)t)
return true;
window.insert(nums[i]);
}
return false;
}
};
// class Solution {
// public:
// bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t)
// {
// double x;
// for(int i=0;i<nums.size();i++)
// {
// for(int j=i+1;j<nums.size();j++)
// {
// x=0.0+nums[i]-nums[j];
// if(x<0)
// x=-1*x;
// if(i!=j && x<=t && abs(i-j)<=k)
// return true;
// }
// }
// return false;
// }
// };