-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDay-02-Contains Duplicate III.cpp
47 lines (35 loc) · 1.33 KB
/
Day-02-Contains 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
Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute difference between i and j is at most k.
Example 1:
Input: nums = [1,2,3,1], k = 3, t = 0
Output: true
Example 2:
Input: nums = [1,0,1,1], k = 1, t = 2
Output: true
Example 3:
Input: nums = [1,5,9,1,5,9], k = 2, t = 3
Output: false
Hide Hint #1
Time complexity O(n logk) - This will give an indication that sorting is involved for k elements.
Hide Hint #2
Use already existing state to evaluate next state - Like, a set of k sorted numbers are only needed to be tracked. When we are processing the next number in array, then we can utilize the existing sorted state and it is not necessary to sort next overlapping set of k numbers again.
// O(n2)
class Solution {
public:
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
vector<vector<int>> v;
for(int i=0;i<nums.size();i++)
{
v.push_back({nums[i],i});
}
sort(v.begin(),v.end());
for(int i=0;i<v.size();i++)
{
for(int j=i+1;j<v.size();j++)
{
if(v[j][0]>v[i][0]+t) break;
if(abs(v[j][1]-v[i][1])<=k) return 1;
}
}
return 0;
}
};