Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3f79d81

Browse files
authoredNov 25, 2019
Create 1042-flower-planting-with-no-adjacent.js
1 parent 71c12df commit 3f79d81

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
 
+36
Original file line numberDiff line numberDiff line change
@@ -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+
for (let i = 0; i < N; i++) {
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

Comments
 (0)
Please sign in to comment.