-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContainsDuplicate.cpp
More file actions
42 lines (40 loc) · 962 Bytes
/
ContainsDuplicate.cpp
File metadata and controls
42 lines (40 loc) · 962 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
35
36
37
38
39
40
41
42
#include<iostream>
#include<vector>
#include<unordered_map>
using namespace std;
// todo: Given an integer array nums and an integer k, return
// true if there are two distinct indices i and j in the array such that nums[i] == nums[j]
// and abs(i - j) <= k.
bool containsNearbyDuplicate(vector<int>& nums, int k)
{
if(k==0)
return false;
unordered_map<int, int> m;
int prevSize = 0;
for(int i=0; i<nums.size(); i++)
{
m.insert(pair(nums[i], i));
if(m.size()==prevSize)
{
if(i-m[nums[i]]<=k)
return true;
else
{
m.erase(nums[i]);
m.insert(pair(nums[i], i));
}
prevSize--;
}
prevSize++;
}
return false;
}
int main()
{
vector<int> v(5);
for(int i=0; i<v.size(); i++)
cin >> v[i];
cin>>k;
cout << containsNearbyDuplicate(v, k);
}
// LC: Q.219