We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 71c12df commit 3f79d81Copy full SHA for 3f79d81
1042-flower-planting-with-no-adjacent.js
@@ -0,0 +1,36 @@
1
+/**
2
+ * @param {number} N
3
+ * @param {number[][]} paths
4
+ * @return {number[]}
5
+ */
6
+const gardenNoAdj = function(N, paths) {
7
+ let map = {};
8
+ for (let i = 0; i < N; i++) {
9
+ map[i] = [];
10
+ }
11
+
12
+ for (let path of paths) {
13
+ let lhs = path[0] - 1;
14
+ let rhs = path[1] - 1;
15
+ map[lhs].push(rhs);
16
+ map[rhs].push(lhs);
17
18
19
+ let result = new Array(N).fill(-1);
20
21
+ let colors = new Array(4).fill(false);
22
+ for (let neighbor of map[i]) {
23
+ if (result[neighbor] !== -1) {
24
+ colors[result[neighbor] - 1] = true;
25
26
27
+ for (let j = 0; j < colors.length; j++) {
28
+ if (!colors[j]) {
29
+ result[i] = j + 1;
30
+ break;
31
32
33
34
35
+ return result;
36
+};
0 commit comments