Skip to content

Commit 59edbd3

Browse files
committed
Runtime: 258 ms (Top 54.35%) | Memory: 69.1 MB (Top 14.13%)
1 parent 0a8bfd7 commit 59edbd3

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Runtime: 258 ms (Top 54.35%) | Memory: 69.1 MB (Top 14.13%)
12
/**
23
* @param {number} n
34
* @param {number[][]} paths
@@ -16,73 +17,72 @@ var gardenNoAdj = function(n, paths) {
1617
}
1718
//if there are no gardens connecting to each other then just return an array of size n with ones
1819
if(paths.length === 0)return new Array(n).fill(1)
19-
20+
2021
//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 flowers
2122
//to solve
2223
let adjList = {}
2324
// create an array filled with ones so if there is a garden with no path just fill with one
24-
let result = new Array(n).fill(1)
25+
let result = new Array(n).fill(1)
2526
let garden = new Set([1,2,3,4])
26-
27+
2728
//create adj list from the paths
2829
paths.forEach(path => {
2930
let first = path[0];
3031
let second = path[1];
31-
32+
3233
if(first in adjList )adjList[first].neighbors.add(second)
3334
else {adjList[first] = new Fnode(second)}
34-
35+
3536
if(second in adjList)adjList[second].neighbors.add(first)
3637
else {adjList[second] = new Fnode(first)}
37-
38+
3839
})
39-
40+
4041
for(let node in adjList){
4142
//every node
4243
let current = adjList[node]
43-
44+
4445
//create a set of invalid flowers, flowers you can't use because they are part of
4546
//the other gardens this one is connected to.
4647
let invalidFlower = new Set();
47-
48+
4849
//iterate over the neighbors to find what type of flower they have and if it is not
4950
//null included in the invalid flowers set
5051
current.neighbors.forEach(neighbor => {
5152
if(adjList[neighbor]['flower'] !== null){
5253
invalidFlower.add(adjList[neighbor]['flower'])
5354
}
5455
})
55-
56+
5657
//create our possible value or better said our value because we will use the first one
5758
//we obtain.
5859
let possibleFlower;
59-
60+
6061
//we iterate over over our garden {1,2,3,4}
6162
for(let flower of garden) {
6263
//and if our flower is not part of the invalid flowers;
6364
if(!invalidFlower.has(flower)){
6465
//we have found a possible flower we can use
6566
possibleFlower = flower
66-
//we break because this is the only one we need
67+
//we break because this is the only one we need
6768
break;
6869
}
6970
}
7071
// we add our flower to current so that we dont use it in the future
7172
current.flower = possibleFlower
72-
73+
7374
//and update our result
7475
result[node - 1] = possibleFlower
7576
}
76-
//we return our result
77+
//we return our result
7778
return result
78-
79-
79+
8080
};
81-
81+
8282
//create a Flower class just for simplicity
8383
class Fnode{
8484
constructor(neighbor){
8585
this.flower = null
8686
this.neighbors = new Set([neighbor])
8787
}
88-
}
88+
}

0 commit comments

Comments
 (0)