-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1182.cpp
More file actions
42 lines (35 loc) · 971 Bytes
/
Copy path1182.cpp
File metadata and controls
42 lines (35 loc) · 971 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
class Solution {
public:
vector<int> shortestDistanceColor(vector<int>& colors, vector<vector<int>>& queries) {
for(int i = 0; i < colors.size(); ++i)
{
m[colors[i]].push_back(i);
}
vector<int> ret;
for(int i = 0; i < queries.size(); ++i)
{
int j = queries[i][0];
int c = queries[i][1];
if(m[c].size() == 0)
{
ret.push_back(-1);
continue;
}
auto w = lower_bound(m[c].begin(), m[c].end(), j);
if(w == m[c].begin())
{
ret.push_back(*w - j);
}
else if(w == m[c].end())
{
ret.push_back(j - m[c][m[c].size()-1]);
}
else
{
ret.push_back( j-*(w-1) < *w-j ? j-*(w-1) : *w-j ) ;
}
}
return ret;
}
map<int, vector<int> > m;
};