class Solution {
public:
vector<int> gardenNoAdj(int n, vector<vector<int>>& paths) {
vector<vector<int>> G(n, vector<int>());
for (int i = 0;i < paths.size();i++)
{
G[paths[i][0] - 1].push_back(paths[i][1] - 1);
G[paths[i][1] - 1].push_back(paths[i][0] - 1);
}
vector<int> ans(n, 0);
for (int i = 0; i < n;i++)
{
set<int> color{ 1,2,3,4 };
for (int j = 0; j < G[i].size();j++)
{
color.erase(ans[G[i][j]]);
}
ans[i] = *(color.begin());
}
return ans;
}
};
LeetCode1042. 不邻接植花
最新推荐文章于 2022-06-30 10:28:42 发布