-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLC_1042.cpp
35 lines (34 loc) · 934 Bytes
/
LC_1042.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
class Solution {
public:
vector<int> gardenNoAdj(int N, vector<vector<int>>& paths) {
vector<vector<int>>graph(N+1);
vector<int>options = {1,2,3,4};
int i,sz=paths.size(),src,parent,neighbor,col,j,k;
bool found;
for(i=0;i<sz;i++)
{
graph[paths[i][0]-1].push_back(paths[i][1]-1);
graph[paths[i][1]-1].push_back(paths[i][0]-1);
}
vector<int>res(N,-1);
for(i=0;i<N;i++)
{
for(k=0;k<4;k++)
{
col = options[k];
found=false;
for(j=0;j<graph[i].size();j++)
{
if((res[graph[i][j]]!=-1) && (res[graph[i][j]]==col))
found=true;
}
if(!found)
{
res[i]=col;
break;
}
}
}
return res;
}
};