-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathLC0220.cpp
More file actions
executable file
·34 lines (30 loc) · 853 Bytes
/
LC0220.cpp
File metadata and controls
executable file
·34 lines (30 loc) · 853 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
/*
Problem Statement: https://leetcode.com/problems/contains-duplicate-iii/
Time: O(n)
Space: O(k)
Author: Mohammed Shoaib, github.com/Mohammed-Shoaib
*/
class Solution {
public:
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
int n = nums.size();
unordered_map<long long, long long> buckets;
// helper function
auto search = [&](int pos, long long bucket) {
auto it = buckets.find(bucket);
return (it != buckets.end() && abs(nums[pos] - it->second) <= t);
};
auto get_bucket = [&](int i) {
return nums[i] / (t + (t == 0));
};
for (int i = 0; i < n; i++) {
long long bucket = get_bucket(i);
if (search(i, bucket) || search(i, bucket - 1) || search(i, bucket + 1))
return true;
buckets[bucket] = nums[i];
if (i >= k)
buckets.erase(get_bucket(i - k));
}
return false;
}
};