-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathflower_planting_with_no_adjacent.cpp
39 lines (39 loc) · 1.29 KB
/
flower_planting_with_no_adjacent.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
class Solution {
public:
vector<int> gardenNoAdj(int N, vector<vector<int>>& paths) {
//garden, flower
unordered_map <int, vector<int>> adj_list;
for(int i=0; i<paths.size();i++)
{
adj_list[paths[i][0]].push_back(paths[i][1]);
adj_list[paths[i][1]].push_back(paths[i][0]);
}
//adj_list contains all paths from each node
unordered_map<int,int> garden_flower;
vector <int> res;
for(int garden = 1; garden<=N; garden++)
{
//which flower to plant in garden = 1
for(int flower=1; flower<=4; flower++)
{
// consider all flowers for garden 1 st no neighbouring garden has flower 1
bool found = false;
for(int neighbour:adj_list[garden])
{
if(garden_flower[neighbour]==flower)
{
found = true; //flower 1 is present in 1 of the neighbours of garden 1
break;
}
}
if(!found)
{
garden_flower[garden] = flower;
res.push_back(flower);
break;
}
}
}
return res;
}
};