Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Flower Planting With No Adjacent

XPChallenge Points: 20
levelLevel: Medium

You are given n gardens labeled from 1 to n, and an array paths where each element paths[i] = [xᵢ, yᵢ] represents a bidirectional path between garden xᵢ and garden yᵢ.
Each garden can be planted with one of four types of flowers (represented by numbers 1, 2, 3, 4).
Your goal is to assign a flower type to each garden such that:
• No two connected gardens (with a path between them) have the same flower type.
• Each garden must have exactly one flower type.
It is guaranteed that a valid assignment exists since each garden connects to at most three others.

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]

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result