forked from shruti170901/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDuplicate III.cpp
52 lines (50 loc) · 1.24 KB
/
Duplicate III.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
class Solution
{
public:
static bool pcomp(pair<int, int> a, pair<int, int> b)
{
return a.first < b.first;
}
bool search(vector<pair<int, int>> l, int t, int k)
{
int po = 0;
while (po < l.size())
{
int i = po + 1;
while (i < l.size())
{
if ((abs(long(l[i].first) - long(l[po].first)) <= t) && (abs(l[i].second - l[po].second) <= k))
{
return true;
}
else
{
if (abs(long(l[i].first) - long(l[po].first)) > t)
{
break;
}
else
{
i += 1;
}
}
}
po += 1;
}
return false;
}
bool containsNearbyAlmostDuplicate(vector<int> &nums, int k, int t)
{
if (nums.size() == 0)
{
return false;
}
vector<pair<int, int>> pp;
for (int i = 0; i < nums.size(); i++)
{
pp.push_back(make_pair(nums[i], i));
}
sort(pp.begin(), pp.end(), pcomp);
return search(pp, t, k);
}
};