forked from illuz/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAC_set_nlogn.cpp
40 lines (35 loc) · 941 Bytes
/
AC_set_nlogn.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
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_set_nlogn.cpp
* Create Date: 2015-07-16 10:17:17
* Descripton:
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 0;
class Solution {
public:
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
set<long long> wnd;
for (int i = 0; i < nums.size(); ++i) {
// keep size
if (i > k)
wnd.erase(nums[i - k - 1]);
auto pos = wnd.lower_bound((long long)nums[i] - t);
if (pos != wnd.end() && *pos <= (long long)nums[i] + t)
return true;
wnd.insert(nums[i]);
}
return false;
}
};
int main() {
Solution s;
int n, k, t;
cin >> n >> k >> t;
vector<int> nums(n);
for (int i = 0; i < n; ++i)
cin >> nums[i];
cout << s.containsNearbyAlmostDuplicate(nums, k, t) << endl;
return 0;
}