You have N
gardens, labelled 1
to N
. In each garden, you want to plant one of 4 types of flowers.
paths[i] = [x, y]
describes the existence of a bidirectional path from garden x
to garden y
.
Also, there is no garden that has more than 3 paths coming into or leaving it.
Your task is to choose a flower type for each garden such that, for any two gardens connected by a path, they have different types of flowers.
Return any such a choice as an array answer
, where answer[i]
is the type of flower planted in the (i+1)
-th garden. The flower types are denoted 1, 2, 3, or 4. It is guaranteed an answer exists.
Example 1:
Input: N = 3, paths = [[1,2],[2,3],[3,1]]
Output: [1,2,3]
Example 2:
Input: N = 4, paths = [[1,2],[3,4]]
Output: [1,2,1,2]
Example 3:
Input: N = 4, paths = [[1,2],[2,3],[3,4],[4,1],[1,3],[2,4]]
Output: [1,2,3,4]
Note:
1 <= N <= 10000
0 <= paths.size <= 20000
- No garden has 4 or more paths coming into or leaving it.
- It is guaranteed an answer exists.
题目大意:
给出N个花园,并给出花园与花园之间相连接的路径,花的种类为1,2,3,4四种,要求相邻之间花的种类不同。
解题思路:
字典存储相连路径,花的种类用一个set存储。
pop() 方法用于随机移除一个元素,满足题设即可。
不要把本题想的过于复杂,每个节点遍历与其相邻结点,随机赋未出现过的种类即可。
class Solution:
def gardenNoAdj(self, N: int, paths: List[List[int]]) -> List[int]:
g = collections.defaultdict(list)
for x,y in paths:
g[x].append(y)
g[y].append(x)
plantdict = {i:0 for i in range(1, N+1)}
for garden in g:
pick = set(range(1,5))
for each in g[garden]:
if plantdict[each] != 0 and plantdict[each] in pick:
pick.remove(plantdict[each])
plantdict[garden] = pick.pop()
return [plantdict[x] if plantdict[x]!=0 else 1 for x in range(1, N+1)]