Flower Planting With No Adjacent LeetCode solution in Python
1class Solution:2def gardenNoAdj(self, n: int, paths: List[List[int]]) -> List[int]:3g = defaultdict(list)4for u, v in paths:5g[u - 1].append(v - 1)6g[v - 1].append(u - 1)7ans = [0] * n8for i in range(n):9c = [1, 2, 3, 4]10for j in g[i]:11if ans[j] in c:12c.remove(ans[j])13ans[i] = c.pop()14return ans