Skip to content

Instantly share code, notes, and snippets.

@s4553711
Created January 9, 2018 14:49
Show Gist options
  • Save s4553711/4224f39d95c46302c184700c2a94b165 to your computer and use it in GitHub Desktop.
Save s4553711/4224f39d95c46302c184700c2a94b165 to your computer and use it in GitHub Desktop.
class Solution {
public:
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
map<long, int> m;
int j = 0;
for(int i = 0; i < nums.size(); i++) {
if (i - j > k && m[nums[j]] == j) m.erase(nums[j++]);
auto a = m.lower_bound((long)nums[i] - t);
if (a != m.end() && abs(a->first - nums[i]) <= t) return true;
m[nums[i]] = i;
}
return false;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment