3
* @param {number[][]} paths6
var gardenNoAdj = function (n, paths) {7
//if n is less than or equal to four just return an array of size n from 1 to n11
while (arr.length < n) {17
//if there are no gardens connecting to each other then just return an array of size n with ones18
if (paths.length === 0) return new Array(n).fill(1);20
//because we have three possible paths connecting to gardens and 4 flowers we know that this problems is possible because our graph is of degree 3 and we have D + 1 flowers23
// create an array filled with ones so if there is a garden with no path just fill with one24
let result = new Array(n).fill(1);25
let garden = new Set([1, 2, 3, 4]);27
//create adj list from the paths28
paths.forEach((path) => {32
if (first in adjList) adjList[first].neighbors.add(second);34
adjList[first] = new Fnode(second);37
if (second in adjList) adjList[second].neighbors.add(first);39
adjList[second] = new Fnode(first);43
for (let node in adjList) {45
let current = adjList[node];47
//create a set of invalid flowers, flowers you can't use because they are part of48
//the other gardens this one is connected to.49
let invalidFlower = new Set();51
//iterate over the neighbors to find what type of flower they have and if it is not52
//null included in the invalid flowers set53
current.neighbors.forEach((neighbor) => {54
if (adjList[neighbor]["flower"] !== null) {55
invalidFlower.add(adjList[neighbor]["flower"]);59
//create our possible value or better said our value because we will use the first one63
//we iterate over over our garden {1,2,3,4}64
for (let flower of garden) {65
//and if our flower is not part of the invalid flowers;66
if (!invalidFlower.has(flower)) {67
//we have found a possible flower we can use68
possibleFlower = flower;69
//we break because this is the only one we need73
// we add our flower to current so that we dont use it in the future74
current.flower = possibleFlower;76
//and update our result77
result[node - 1] = possibleFlower;79
//we return our result83
//create a Flower class just for simplicity85
constructor(neighbor) {87
this.neighbors = new Set([neighbor]);