59
59
60
60
<!-- 这里可写通用的实现逻辑 -->
61
61
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
+
62
72
<!-- tabs:start -->
63
73
64
74
### ** Python3**
@@ -74,11 +84,11 @@ class Solution:
74
84
g[x].append(y)
75
85
g[y].append(x)
76
86
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]}
79
89
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
82
92
break
83
93
return ans
84
94
```
@@ -92,20 +102,21 @@ class Solution {
92
102
public int [] gardenNoAdj (int n , int [][] paths ) {
93
103
List<Integer > [] g = new List [n];
94
104
Arrays . setAll(g, k - > new ArrayList<> ());
95
- for (int [] p : paths) {
105
+ for (var p : paths) {
96
106
int x = p[0 ] - 1 , y = p[1 ] - 1 ;
97
107
g[x]. add(y);
98
108
g[y]. add(x);
99
109
}
100
110
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 ;
105
116
}
106
117
for (int c = 1 ; c < 5 ; ++ c) {
107
- if (! colors . contains(c) ) {
108
- ans[u ] = c;
118
+ if (! used[c] ) {
119
+ ans[x ] = c;
109
120
break ;
110
121
}
111
122
}
@@ -128,12 +139,15 @@ public:
128
139
g[ y] .push_back(x);
129
140
}
130
141
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
+ }
134
148
for (int c = 1; c < 5; ++c) {
135
- if (!colors.count(c) ) {
136
- ans[ u ] = c;
149
+ if (!used [ c ] ) {
150
+ ans[ x ] = c;
137
151
break;
138
152
}
139
153
}
@@ -154,14 +168,14 @@ func gardenNoAdj(n int, paths [][]int) []int {
154
168
g[y] = append(g[y], x)
155
169
}
156
170
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
161
175
}
162
176
for c := 1; c < 5; c++ {
163
- if !colors [c] {
164
- ans[u ] = c
177
+ if !used [c] {
178
+ ans[x ] = c
165
179
break
166
180
}
167
181
}
@@ -170,6 +184,32 @@ func gardenNoAdj(n int, paths [][]int) []int {
170
184
}
171
185
```
172
186
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
+
173
213
### ** ...**
174
214
175
215
```
0 commit comments