Skip to content

Commit 0dc873c

Browse files
committed
feat: add solutions to lc problem: No.1042
No.1042.Flower Planting With No Adjacent
1 parent 49e2e8e commit 0dc873c

File tree

7 files changed

+161
-66
lines changed

7 files changed

+161
-66
lines changed

solution/1000-1099/1042.Flower Planting With No Adjacent/README.md

+62-22
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@
5959

6060
<!-- 这里可写通用的实现逻辑 -->
6161

62+
**方法一:枚举**
63+
64+
我们先根据数组 $paths$ 构建图 $g$,其中 $g[x]$ 表示与花园 $x$ 相邻的花园列表。
65+
66+
接下来,对于每个花园 $x$,我们先找出与 $x$ 相邻的花园 $y$,并将 $y$ 花园中种植的花的种类标记为已使用。然后我们从花的种类 $1$ 开始枚举,直到找到一个未被使用的花的种类 $c$,将 $c$ 标记为 $x$ 花园中种植的花的种类,然后继续枚举下一个花园。
67+
68+
枚举结束后,返回答案即可。
69+
70+
时间复杂度 $O(n + m)$,空间复杂度 $O(n + m)$。其中 $n$ 和 $m$ 分别是花园的数量和路径的数量。
71+
6272
<!-- tabs:start -->
6373

6474
### **Python3**
@@ -74,11 +84,11 @@ class Solution:
7484
g[x].append(y)
7585
g[y].append(x)
7686
ans = [0] * n
77-
for u in range(n):
78-
colors = set(ans[v] for v in g[u])
87+
for x in range(n):
88+
used = {ans[y] for y in g[x]}
7989
for c in range(1, 5):
80-
if c not in colors:
81-
ans[u] = c
90+
if c not in used:
91+
ans[x] = c
8292
break
8393
return ans
8494
```
@@ -92,20 +102,21 @@ class Solution {
92102
public int[] gardenNoAdj(int n, int[][] paths) {
93103
List<Integer>[] g = new List[n];
94104
Arrays.setAll(g, k -> new ArrayList<>());
95-
for (int[] p : paths) {
105+
for (var p : paths) {
96106
int x = p[0] - 1, y = p[1] - 1;
97107
g[x].add(y);
98108
g[y].add(x);
99109
}
100110
int[] ans = new int[n];
101-
for (int u = 0; u < n; ++u) {
102-
Set<Integer> colors = new HashSet<>();
103-
for (int v : g[u]) {
104-
colors.add(ans[v]);
111+
boolean[] used = new boolean[5];
112+
for (int x = 0; x < n; ++x) {
113+
Arrays.fill(used, false);
114+
for (int y : g[x]) {
115+
used[ans[y]] = true;
105116
}
106117
for (int c = 1; c < 5; ++c) {
107-
if (!colors.contains(c)) {
108-
ans[u] = c;
118+
if (!used[c]) {
119+
ans[x] = c;
109120
break;
110121
}
111122
}
@@ -128,12 +139,15 @@ public:
128139
g[y].push_back(x);
129140
}
130141
vector<int> ans(n);
131-
for (int u = 0; u < n; ++u) {
132-
unordered_set<int> colors;
133-
for (int v : g[u]) colors.insert(ans[v]);
142+
bool used[5];
143+
for (int x = 0; x < n; ++x) {
144+
memset(used, false, sizeof(used));
145+
for (int y : g[x]) {
146+
used[ans[y]] = true;
147+
}
134148
for (int c = 1; c < 5; ++c) {
135-
if (!colors.count(c)) {
136-
ans[u] = c;
149+
if (!used[c]) {
150+
ans[x] = c;
137151
break;
138152
}
139153
}
@@ -154,14 +168,14 @@ func gardenNoAdj(n int, paths [][]int) []int {
154168
g[y] = append(g[y], x)
155169
}
156170
ans := make([]int, n)
157-
for u := 0; u < n; u++ {
158-
colors := make(map[int]bool)
159-
for _, v := range g[u] {
160-
colors[ans[v]] = true
171+
for x := 0; x < n; x++ {
172+
used := [5]bool{}
173+
for _, y := range g[x] {
174+
used[ans[y]] = true
161175
}
162176
for c := 1; c < 5; c++ {
163-
if !colors[c] {
164-
ans[u] = c
177+
if !used[c] {
178+
ans[x] = c
165179
break
166180
}
167181
}
@@ -170,6 +184,32 @@ func gardenNoAdj(n int, paths [][]int) []int {
170184
}
171185
```
172186

187+
### **TypeScript**
188+
189+
```ts
190+
function gardenNoAdj(n: number, paths: number[][]): number[] {
191+
const g: number[][] = new Array(n).fill(0).map(() => []);
192+
for (const [x, y] of paths) {
193+
g[x - 1].push(y - 1);
194+
g[y - 1].push(x - 1);
195+
}
196+
const ans: number[] = new Array(n).fill(0);
197+
for (let x = 0; x < n; ++x) {
198+
const used: boolean[] = new Array(5).fill(false);
199+
for (const y of g[x]) {
200+
used[ans[y]] = true;
201+
}
202+
for (let c = 1; c < 5; ++c) {
203+
if (!used[c]) {
204+
ans[x] = c;
205+
break;
206+
}
207+
}
208+
}
209+
return ans;
210+
}
211+
```
212+
173213
### **...**
174214

175215
```

solution/1000-1099/1042.Flower Planting With No Adjacent/README_EN.md

+52-22
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ class Solution:
6666
g[x].append(y)
6767
g[y].append(x)
6868
ans = [0] * n
69-
for u in range(n):
70-
colors = set(ans[v] for v in g[u])
69+
for x in range(n):
70+
used = {ans[y] for y in g[x]}
7171
for c in range(1, 5):
72-
if c not in colors:
73-
ans[u] = c
72+
if c not in used:
73+
ans[x] = c
7474
break
7575
return ans
7676
```
@@ -82,20 +82,21 @@ class Solution {
8282
public int[] gardenNoAdj(int n, int[][] paths) {
8383
List<Integer>[] g = new List[n];
8484
Arrays.setAll(g, k -> new ArrayList<>());
85-
for (int[] p : paths) {
85+
for (var p : paths) {
8686
int x = p[0] - 1, y = p[1] - 1;
8787
g[x].add(y);
8888
g[y].add(x);
8989
}
9090
int[] ans = new int[n];
91-
for (int u = 0; u < n; ++u) {
92-
Set<Integer> colors = new HashSet<>();
93-
for (int v : g[u]) {
94-
colors.add(ans[v]);
91+
boolean[] used = new boolean[5];
92+
for (int x = 0; x < n; ++x) {
93+
Arrays.fill(used, false);
94+
for (int y : g[x]) {
95+
used[ans[y]] = true;
9596
}
9697
for (int c = 1; c < 5; ++c) {
97-
if (!colors.contains(c)) {
98-
ans[u] = c;
98+
if (!used[c]) {
99+
ans[x] = c;
99100
break;
100101
}
101102
}
@@ -118,12 +119,15 @@ public:
118119
g[y].push_back(x);
119120
}
120121
vector<int> ans(n);
121-
for (int u = 0; u < n; ++u) {
122-
unordered_set<int> colors;
123-
for (int v : g[u]) colors.insert(ans[v]);
122+
bool used[5];
123+
for (int x = 0; x < n; ++x) {
124+
memset(used, false, sizeof(used));
125+
for (int y : g[x]) {
126+
used[ans[y]] = true;
127+
}
124128
for (int c = 1; c < 5; ++c) {
125-
if (!colors.count(c)) {
126-
ans[u] = c;
129+
if (!used[c]) {
130+
ans[x] = c;
127131
break;
128132
}
129133
}
@@ -144,14 +148,14 @@ func gardenNoAdj(n int, paths [][]int) []int {
144148
g[y] = append(g[y], x)
145149
}
146150
ans := make([]int, n)
147-
for u := 0; u < n; u++ {
148-
colors := make(map[int]bool)
149-
for _, v := range g[u] {
150-
colors[ans[v]] = true
151+
for x := 0; x < n; x++ {
152+
used := [5]bool{}
153+
for _, y := range g[x] {
154+
used[ans[y]] = true
151155
}
152156
for c := 1; c < 5; c++ {
153-
if !colors[c] {
154-
ans[u] = c
157+
if !used[c] {
158+
ans[x] = c
155159
break
156160
}
157161
}
@@ -160,6 +164,32 @@ func gardenNoAdj(n int, paths [][]int) []int {
160164
}
161165
```
162166

167+
### **TypeScript**
168+
169+
```ts
170+
function gardenNoAdj(n: number, paths: number[][]): number[] {
171+
const g: number[][] = new Array(n).fill(0).map(() => []);
172+
for (const [x, y] of paths) {
173+
g[x - 1].push(y - 1);
174+
g[y - 1].push(x - 1);
175+
}
176+
const ans: number[] = new Array(n).fill(0);
177+
for (let x = 0; x < n; ++x) {
178+
const used: boolean[] = new Array(5).fill(false);
179+
for (const y of g[x]) {
180+
used[ans[y]] = true;
181+
}
182+
for (let c = 1; c < 5; ++c) {
183+
if (!used[c]) {
184+
ans[x] = c;
185+
break;
186+
}
187+
}
188+
}
189+
return ans;
190+
}
191+
```
192+
163193
### **...**
164194

165195
```

solution/1000-1099/1042.Flower Planting With No Adjacent/Solution.cpp

+8-5
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ class Solution {
88
g[y].push_back(x);
99
}
1010
vector<int> ans(n);
11-
for (int u = 0; u < n; ++u) {
12-
unordered_set<int> colors;
13-
for (int v : g[u]) colors.insert(ans[v]);
11+
bool used[5];
12+
for (int x = 0; x < n; ++x) {
13+
memset(used, false, sizeof(used));
14+
for (int y : g[x]) {
15+
used[ans[y]] = true;
16+
}
1417
for (int c = 1; c < 5; ++c) {
15-
if (!colors.count(c)) {
16-
ans[u] = c;
18+
if (!used[c]) {
19+
ans[x] = c;
1720
break;
1821
}
1922
}

solution/1000-1099/1042.Flower Planting With No Adjacent/Solution.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ func gardenNoAdj(n int, paths [][]int) []int {
66
g[y] = append(g[y], x)
77
}
88
ans := make([]int, n)
9-
for u := 0; u < n; u++ {
10-
colors := make(map[int]bool)
11-
for _, v := range g[u] {
12-
colors[ans[v]] = true
9+
for x := 0; x < n; x++ {
10+
used := [5]bool{}
11+
for _, y := range g[x] {
12+
used[ans[y]] = true
1313
}
1414
for c := 1; c < 5; c++ {
15-
if !colors[c] {
16-
ans[u] = c
15+
if !used[c] {
16+
ans[x] = c
1717
break
1818
}
1919
}

solution/1000-1099/1042.Flower Planting With No Adjacent/Solution.java

+8-7
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,21 @@ class Solution {
22
public int[] gardenNoAdj(int n, int[][] paths) {
33
List<Integer>[] g = new List[n];
44
Arrays.setAll(g, k -> new ArrayList<>());
5-
for (int[] p : paths) {
5+
for (var p : paths) {
66
int x = p[0] - 1, y = p[1] - 1;
77
g[x].add(y);
88
g[y].add(x);
99
}
1010
int[] ans = new int[n];
11-
for (int u = 0; u < n; ++u) {
12-
Set<Integer> colors = new HashSet<>();
13-
for (int v : g[u]) {
14-
colors.add(ans[v]);
11+
boolean[] used = new boolean[5];
12+
for (int x = 0; x < n; ++x) {
13+
Arrays.fill(used, false);
14+
for (int y : g[x]) {
15+
used[ans[y]] = true;
1516
}
1617
for (int c = 1; c < 5; ++c) {
17-
if (!colors.contains(c)) {
18-
ans[u] = c;
18+
if (!used[c]) {
19+
ans[x] = c;
1920
break;
2021
}
2122
}

solution/1000-1099/1042.Flower Planting With No Adjacent/Solution.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ def gardenNoAdj(self, n: int, paths: List[List[int]]) -> List[int]:
66
g[x].append(y)
77
g[y].append(x)
88
ans = [0] * n
9-
for u in range(n):
10-
colors = set(ans[v] for v in g[u])
9+
for x in range(n):
10+
used = {ans[y] for y in g[x]}
1111
for c in range(1, 5):
12-
if c not in colors:
13-
ans[u] = c
12+
if c not in used:
13+
ans[x] = c
1414
break
1515
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function gardenNoAdj(n: number, paths: number[][]): number[] {
2+
const g: number[][] = new Array(n).fill(0).map(() => []);
3+
for (const [x, y] of paths) {
4+
g[x - 1].push(y - 1);
5+
g[y - 1].push(x - 1);
6+
}
7+
const ans: number[] = new Array(n).fill(0);
8+
for (let x = 0; x < n; ++x) {
9+
const used: boolean[] = new Array(5).fill(false);
10+
for (const y of g[x]) {
11+
used[ans[y]] = true;
12+
}
13+
for (let c = 1; c < 5; ++c) {
14+
if (!used[c]) {
15+
ans[x] = c;
16+
break;
17+
}
18+
}
19+
}
20+
return ans;
21+
}

0 commit comments

Comments
 (0)