Algorithm


Problem Name: 1042. Flower Planting With No Adjacent

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.

 

Example 1:

Input: n = 3, paths = [[1,2],[2,3],[3,1]]
Output: [1,2,3]
Explanation:
Gardens 1 and 2 have different types.
Gardens 2 and 3 have different types.
Gardens 3 and 1 have different types.
Hence, [1,2,3] is a valid answer. Other valid answers include [1,2,4], [1,4,2], and [3,2,1].

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]

 

Constraints:

  • 1 <= n <= 104
  • 0 <= paths.length <= 2 * 104
  • paths[i].length == 2
  • 1 <= xi, yi <= n
  • xi != yi
  • Every garden has at most 3 paths coming into or leaving it.

Code Examples

#1 Code Example with Javascript Programming

Code - Javascript Programming


const gardenNoAdj = function(N, paths) {
  const map = {};
  for (let i = 0; i  <  N; i++) {
    map[i] = [];
  }
  for (let path of paths) {
    let l = path[0] - 1;
    let r = path[1] - 1;
    map[l].push(r);
    map[r].push(l);
  }
  const result = new Array(N).fill(-1);
  for (let i = 0; i  <  N; i++) {
    let colors = new Array(4).fill(false);
    for (let neighbor of map[i]) {
      if (result[neighbor] !== -1) {
        colors[result[neighbor]] = true;
      }
    }
    for (let j = 0; j  <  colors.length; j++) {
      if (!colors[j]) {
        result[i] = j;
        break;
      }
    }
  }
  return result.map(i => ++i);
};
Copy The Code & Try With Live Editor

Input

x
+
cmd
n = 4, paths = [[1,2],[3,4]]

Output

x
+
cmd
[1,2,1,2]

#2 Code Example with Python Programming

Code - Python Programming


class Solution:
    def gardenNoAdj(self, N: int, paths: List[List[int]]) -> List[int]:
        res = [0] * N
        G = [[] for i in range(N)]
        for x, y in paths:
            G[x - 1].append(y - 1)
            G[y - 1].append(x - 1)
        for i in range(N):
            res[i] = ({1, 2, 3, 4} - {res[j] for j in G[i]}).pop()
        return res
Copy The Code & Try With Live Editor

Input

x
+
cmd
n = 4, paths = [[1,2],[3,4]]

Output

x
+
cmd
[1,2,1,2]

#3 Code Example with C# Programming

Code - C# Programming


using System.Collections.Generic;

namespace LeetCode
{
    public class _1042_FlowerPlantingWithNoAdjacent
    {
        public int[] GardenNoAdj(int N, int[][] paths)
        {
            var edges = new Dictionary < int, IList<int>>();
            for (int i = 0; i  <  N; i++)
                edges[i] = new List<int>();
            foreach (var path in paths)
            {
                edges[path[0] - 1].Add(path[1] - 1);
                edges[path[1] - 1].Add(path[0] - 1);
            }

            var results = new int[N];
            for (int i = 0; i  <  N; i++)
            {
                var usedColor = new bool[5];
                foreach (var v in edges[i])
                    usedColor[results[v]] = true;

                for (int c = 1; c  < = 4; c++)
                    if (!usedColor[c])
                    {
                        results[i] = c;
                        break;
                    }
            }

            return results;
        }
    }
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
n = 4, paths = [[1,2],[3,4]]

Output

x
+
cmd
[1,2,1,2]
Advertisements

Demonstration


Previous
#1041 Leetcode Robot Bounded In Circle Solution in C, C++, Java, JavaScript, Python, C# Leetcode
Next
#1043 Leetcode Partition Array for Maximum Sum Solution in C, C++, Java, JavaScript, Python, C# Leetcode