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 773ae55

Browse files
committedApr 2, 2025
Add solution #1042
1 parent dca3528 commit 773ae55

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,115 LeetCode solutions in JavaScript
1+
# 1,116 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -849,6 +849,7 @@
849849
1039|[Minimum Score Triangulation of Polygon](./solutions/1039-minimum-score-triangulation-of-polygon.js)|Medium|
850850
1040|[Moving Stones Until Consecutive II](./solutions/1040-moving-stones-until-consecutive-ii.js)|Medium|
851851
1041|[Robot Bounded In Circle](./solutions/1041-robot-bounded-in-circle.js)|Medium|
852+
1042|[Flower Planting With No Adjacent](./solutions/1042-flower-planting-with-no-adjacent.js)|Medium|
852853
1047|[Remove All Adjacent Duplicates In String](./solutions/1047-remove-all-adjacent-duplicates-in-string.js)|Easy|
853854
1051|[Height Checker](./solutions/1051-height-checker.js)|Easy|
854855
1071|[Greatest Common Divisor of Strings](./solutions/1071-greatest-common-divisor-of-strings.js)|Easy|
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* 1042. Flower Planting With No Adjacent
3+
* https://leetcode.com/problems/flower-planting-with-no-adjacent/
4+
* Difficulty: Medium
5+
*
6+
* You have n gardens, labeled from 1 to n, and an array paths where paths[i] = [xi, yi] describes
7+
* a bidirectional path between garden xi to garden yi. In each garden, you want to plant one of
8+
* 4 types of flowers.
9+
*
10+
* All gardens have at most 3 paths coming into or leaving it.
11+
*
12+
* Your task is to choose a flower type for each garden such that, for any two gardens connected
13+
* by a path, they have different types of flowers.
14+
*
15+
* Return any such a choice as an array answer, where answer[i] is the type of flower planted in
16+
* the (i+1)th garden. The flower types are denoted 1, 2, 3, or 4. It is guaranteed an answer
17+
* exists.
18+
*/
19+
20+
/**
21+
* @param {number} n
22+
* @param {number[][]} paths
23+
* @return {number[]}
24+
*/
25+
var gardenNoAdj = function(n, paths) {
26+
const graph = Array.from({ length: n }, () => new Set());
27+
const result = new Array(n).fill(0);
28+
29+
for (const [x, y] of paths) {
30+
graph[x - 1].add(y - 1);
31+
graph[y - 1].add(x - 1);
32+
}
33+
34+
for (let garden = 0; garden < n; garden++) {
35+
const usedColors = new Set();
36+
for (const neighbor of graph[garden]) {
37+
usedColors.add(result[neighbor]);
38+
}
39+
40+
for (let color = 1; color <= 4; color++) {
41+
if (!usedColors.has(color)) {
42+
result[garden] = color;
43+
break;
44+
}
45+
}
46+
}
47+
48+
return result;
49+
};

0 commit comments

Comments
 (0)
Please sign in to comment.