Skip to content

Commit 1d9583d

Browse files
committed
feat: finish 1042
1 parent 25424e7 commit 1d9583d

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# 1042. Flower Planting With No Adjacent
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Depth-First Search, Breadth-First Search, Graph.
5+
- Similar Questions: .
6+
7+
## Problem
8+
9+
You have ```n``` gardens, labeled from ```1``` to ```n```, and an array ```paths``` where ```paths[i] = [xi, yi]``` describes a bidirectional path between garden ```xi``` to garden ```yi```. In each garden, you want to plant one of 4 types of flowers.
10+
11+
All gardens have **at most 3** paths coming into or leaving it.
12+
13+
Your task is to choose a flower type for each garden such that, for any two gardens connected 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 the **```(i+1)th```** garden. The flower types are denoted **```1```**, **```2```**, **```3```**, or **```4```**. It is guaranteed an answer exists.**
16+
17+
 
18+
Example 1:
19+
20+
```
21+
Input: n = 3, paths = [[1,2],[2,3],[3,1]]
22+
Output: [1,2,3]
23+
Explanation:
24+
Gardens 1 and 2 have different types.
25+
Gardens 2 and 3 have different types.
26+
Gardens 3 and 1 have different types.
27+
Hence, [1,2,3] is a valid answer. Other valid answers include [1,2,4], [1,4,2], and [3,2,1].
28+
```
29+
30+
Example 2:
31+
32+
```
33+
Input: n = 4, paths = [[1,2],[3,4]]
34+
Output: [1,2,1,2]
35+
```
36+
37+
Example 3:
38+
39+
```
40+
Input: n = 4, paths = [[1,2],[2,3],[3,4],[4,1],[1,3],[2,4]]
41+
Output: [1,2,3,4]
42+
```
43+
44+
 
45+
**Constraints:**
46+
47+
48+
49+
- ```1 <= n <= 104```
50+
51+
- ```0 <= paths.length <= 2 * 104```
52+
53+
- ```paths[i].length == 2```
54+
55+
- ```1 <= xi, yi <= n```
56+
57+
- ```xi != yi```
58+
59+
- Every garden has **at most 3** paths coming into or leaving it.
60+
61+
62+
63+
## Solution
64+
65+
```javascript
66+
67+
```
68+
69+
**Explain:**
70+
71+
nope.
72+
73+
**Complexity:**
74+
75+
* Time complexity : O(n).
76+
* Space complexity : O(n).

0 commit comments

Comments
 (0)