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
| class Solution { public: vector<int> gardenNoAdj(int n, vector<vector<int>>& paths) { vector<int> ans(n + 1, 5); vector<vector<int> > vis(n + 1); for(auto& x : paths) { vis[x[0]].push_back(x[1]); vis[x[1]].push_back(x[0]); } for(int i = 1; i <= n; i++) { if(!vis[i].size()) { ans[i] = 1; continue; } vector<int> temp{1,2,3,4}; for(auto& x : vis[i]) { if(ans[x] < 5) { vector<int>::iterator pos = find(temp.begin(), temp.end(), ans[x]); if(pos != temp.end()) temp.erase(pos); } } ans[i] = temp[0]; } for(int i = 0; i < n; i++) ans[i] = ans[i + 1]; ans.pop_back(); return ans; } };
|