forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
56 lines (41 loc) · 1.23 KB
/
main.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
45
46
47
48
49
50
51
52
53
54
55
56
/// Source : https://leetcode.com/problems/contains-duplicate-iii/description/
/// Author : liuyubobobo
/// Time : 2017-11-15
#include <iostream>
#include <vector>
#include <set>
#include <cassert>
#include <stdexcept>
#include <cmath>
using namespace std;
// Using Tree Set
// Time Complexity: O(nlogk)
// Space Complexity: O(k)
class Solution {
public:
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
if(t < 0)
return false;
set<long long> record;
for(int i = 0 ; i < nums.size() ; i ++){
if(record.lower_bound((long long)nums[i] - (long long)t) != record.end() &&
*record.lower_bound((long long)nums[i] - (long long)t ) <= (long long)nums[i] + (long long)t)
return true;
record.insert(nums[i]);
if(record.size() == k + 1)
record.erase( nums[i-k] );
}
return false;
}
};
void printBool(bool b){
cout << (b ? "True" : "False") << endl;
}
int main() {
int nums[] = {-2147483648, -2147483647};
vector<int> vec(nums, nums + sizeof(nums)/sizeof(int));
int k = 3;
int t = 3;
printBool(Solution().containsNearbyAlmostDuplicate(vec, k, t));
return 0;
}