Flower Planting With No Adjacent - Problem

You are a landscape designer tasked with creating a beautiful garden arrangement! You have n gardens labeled from 1 to n, connected by bidirectional paths. Your goal is to plant one of 4 types of flowers (numbered 1, 2, 3, or 4) in each garden.

Here's the challenge: no two gardens connected by a path can have the same type of flower. Think of it as a graph coloring problem where you need to ensure adjacent nodes have different colors!

The good news? Each garden has at most 3 connecting paths, and since you have 4 flower types available, a solution is always guaranteed to exist.

Input: An integer n (number of gardens) and an array paths where paths[i] = [x_i, y_i] represents a bidirectional path between gardens x_i and y_i.

Output: An array answer where answer[i] is the type of flower (1-4) planted in the (i+1)th garden.

Input & Output

example_1.py โ€” Basic Path Chain
$ Input: n = 3, paths = [[1,2],[2,3]]
โ€บ Output: [1,2,1]
๐Ÿ’ก Note: Gardens 1 and 3 can use the same flower (1) since they're not connected. Garden 2 needs a different flower (2) from both neighbors.
example_2.py โ€” Triangle Graph
$ Input: n = 4, paths = [[1,2],[1,3],[2,4]]
โ€บ Output: [1,2,2,1]
๐Ÿ’ก Note: Garden 1 uses flower 1, its neighbors (2,3) must use different flowers. Garden 4 only connects to 2, so it can reuse flower 1.
example_3.py โ€” Single Garden
$ Input: n = 1, paths = []
โ€บ Output: [1]
๐Ÿ’ก Note: With no connections, the single garden can use any flower type. We choose type 1.

Constraints

  • 1 โ‰ค n โ‰ค 104
  • 0 โ‰ค paths.length โ‰ค 2 * 104
  • paths[i].length == 2
  • 1 โ‰ค xi, yi โ‰ค n
  • Each garden has at most 3 paths coming into or leaving it
  • xi โ‰  yi
  • It is guaranteed an answer exists

Visualization

Tap to expand
๐ŸŒป Garden Flower Assignment ๐ŸŒป1๐ŸŒน Red2๐ŸŒธ Blue3๐ŸŒน Red4๐ŸŒผ YellowColor Assignment Strategy๐ŸŒน Type 1: Red Roses๐ŸŒธ Type 2: Blue Hydrangeas๐ŸŒผ Type 3: Yellow Sunflowers๐ŸŒบ Type 4: Pink TulipsGreedy Algorithm1. Garden 1: No neighbors โ†’ Pick Type 1 โœ“2. Garden 2: Neighbor uses 1 โ†’ Pick Type 2 โœ“3. Garden 3: Neighbor uses 2 โ†’ Pick Type 1 โœ“4. Garden 4: Neighbor uses 2 โ†’ Pick Type 1 โœ“Result: [1, 2, 1, 1] - All valid! ๐ŸŽ‰
Understanding the Visualization
1
Map the Neighborhood
Build adjacency list showing which gardens are connected by paths
2
Visit Each Garden
Go through gardens 1, 2, 3, ... in order
3
Check Neighbors
For current garden, see which flower types its neighbors already use
4
Pick First Available
Choose the lowest numbered flower type that neighbors don't use
5
Guaranteed Success
Since max neighbors = 3 and available flowers = 4, always find one
Key Takeaway
๐ŸŽฏ Key Insight: Since each garden has โ‰ค 3 neighbors and we have 4 flower types, greedy assignment always finds a valid color without backtracking!
Asked in
Google 45 Facebook 38 Amazon 32 Microsoft 28
38.4K Views
Medium Frequency
~15 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen