Skip to content

Commit 7d6e3e3

Browse files
committed
Runtime 229 ms (Top 41.75%) | Memory 78.0 MB (Top 68.4%)
1 parent 8266ba8 commit 7d6e3e3

File tree

1 file changed

+29
-44
lines changed

1 file changed

+29
-44
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,29 @@
1-
class Solution {
2-
public:
3-
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t)
4-
{
5-
if(k==0)
6-
return false;
7-
multiset<long> window;
8-
9-
for(int i=0;i<nums.size();i++)
10-
{
11-
if(i>k)
12-
window.erase(nums[i-k-1]);
13-
auto it=window.lower_bound((long)nums[i]-(long)t);
14-
if(it!=window.end() && *it<=(long)nums[i]+(long)t)
15-
return true;
16-
window.insert(nums[i]);
17-
}
18-
return false;
19-
}
20-
};
21-
22-
23-
24-
25-
26-
// class Solution {
27-
// public:
28-
// bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t)
29-
// {
30-
// double x;
31-
// for(int i=0;i<nums.size();i++)
32-
// {
33-
// for(int j=i+1;j<nums.size();j++)
34-
// {
35-
// x=0.0+nums[i]-nums[j];
36-
// if(x<0)
37-
// x=-1*x;
38-
// if(i!=j && x<=t && abs(i-j)<=k)
39-
// return true;
40-
// }
41-
// }
42-
// return false;
43-
// }
44-
// };
1+
class Solution {
2+
public:
3+
bool containsNearbyAlmostDuplicate(vector<int>& nums, int indexDiff, int valueDiff) {
4+
int i=0;
5+
map<int,int> mp;
6+
int n=nums.size();
7+
for(int j=0;j<n;j++){
8+
auto val=mp.lower_bound(nums[j]);
9+
if(val!=mp.end() and (val->first-nums[j])<=valueDiff){
10+
return true;
11+
}
12+
if(val!=mp.begin()){
13+
val--;
14+
if(abs(val->first-nums[j])<=valueDiff){
15+
return true;
16+
}
17+
}
18+
mp[nums[j]]++;
19+
if((j-i)==indexDiff){
20+
mp[nums[i]]--;
21+
if(mp[nums[i]]==0){
22+
mp.erase(nums[i]);
23+
}
24+
i++;
25+
}
26+
}
27+
return false;
28+
}
29+
};

0 commit comments

Comments
 (0)