Practice Flower Planting With No Adjacent
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Flower Planting With No Adjacent
Problem Statement
You have n gardens, labeled from 1 to n, and an array paths where paths[i] = [xi, yi] describes a bidirectional path between garden xi to garden yi. In each garden, you want to plant one of 4 types of flowers. All gardens have at most 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.
Source: Flower Planting With No Adjacent on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
n = 3, paths = [[1,2],[2,3],[3,1]]
[1,2,3]
# Example 2
n = 4, paths = [[1,2],[3,4]]
[1,2,1,2]
# Example 3
n = 4, paths = [[1,2],[2,3],[3,4],[4,1],[1,3],[2,4]]
[1,2,3,4]
Constraints
Related LeetCode Problems
Practice more problems similar to Flower Planting With No Adjacent.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.