Skip to content

Commit 772fa18

Browse files
committed
feat: add solutions to lc problem: No.1042
No.1042.Flower Planting With No Adjacent
1 parent f732cbd commit 772fa18

File tree

6 files changed

+291
-2
lines changed

6 files changed

+291
-2
lines changed

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

+100-1
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,114 @@
6666
<!-- 这里可写当前语言的特殊实现逻辑 -->
6767

6868
```python
69-
69+
class Solution:
70+
def gardenNoAdj(self, n: int, paths: List[List[int]]) -> List[int]:
71+
g = defaultdict(list)
72+
for x, y in paths:
73+
x, y = x - 1, y - 1
74+
g[x].append(y)
75+
g[y].append(x)
76+
ans = [0] * n
77+
for u in range(n):
78+
colors = set(ans[v] for v in g[u])
79+
for c in range(1, 5):
80+
if c not in colors:
81+
ans[u] = c
82+
break
83+
return ans
7084
```
7185

7286
### **Java**
7387

7488
<!-- 这里可写当前语言的特殊实现逻辑 -->
7589

7690
```java
91+
class Solution {
92+
public int[] gardenNoAdj(int n, int[][] paths) {
93+
List<Integer>[] g = new List[n];
94+
for (int i = 0; i < n; ++i) {
95+
g[i] = new ArrayList<>();
96+
}
97+
for (int[] p : paths) {
98+
int x = p[0] - 1, y = p[1] - 1;
99+
g[x].add(y);
100+
g[y].add(x);
101+
}
102+
int[] ans = new int[n];
103+
for (int u = 0; u < n; ++u) {
104+
Set<Integer> colors = new HashSet<>();
105+
for (int v : g[u]) {
106+
colors.add(ans[v]);
107+
}
108+
for (int c = 1; c < 5; ++c) {
109+
if (!colors.contains(c)) {
110+
ans[u] = c;
111+
break;
112+
}
113+
}
114+
}
115+
return ans;
116+
}
117+
}
118+
```
119+
120+
### **C++**
121+
122+
```cpp
123+
class Solution {
124+
public:
125+
vector<int> gardenNoAdj(int n, vector<vector<int>>& paths) {
126+
vector<vector<int>> g(n);
127+
for (auto& p : paths)
128+
{
129+
int x = p[0] - 1, y = p[1] - 1;
130+
g[x].push_back(y);
131+
g[y].push_back(x);
132+
}
133+
vector<int> ans(n);
134+
for (int u = 0; u < n; ++u)
135+
{
136+
unordered_set<int> colors;
137+
for (int v : g[u]) colors.insert(ans[v]);
138+
for (int c = 1; c < 5; ++c)
139+
{
140+
if (!colors.count(c))
141+
{
142+
ans[u] = c;
143+
break;
144+
}
145+
}
146+
}
147+
return ans;
148+
}
149+
};
150+
```
77151
152+
### **Go**
153+
154+
```go
155+
func gardenNoAdj(n int, paths [][]int) []int {
156+
g := make([][]int, n)
157+
for _, p := range paths {
158+
x, y := p[0]-1, p[1]-1
159+
g[x] = append(g[x], y)
160+
g[y] = append(g[y], x)
161+
}
162+
ans := make([]int, n)
163+
for u := 0; u < n; u++ {
164+
colors := make(map[int]bool)
165+
for _, v := range g[u] {
166+
colors[ans[v]] = true
167+
}
168+
for c := 1; c < 5; c++ {
169+
if !colors[c] {
170+
ans[u] = c
171+
break
172+
}
173+
}
174+
}
175+
return ans
176+
}
78177
```
79178

80179
### **...**

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

+100-1
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,112 @@ Hence, [1,2,3] is a valid answer. Other valid answers include [1,2,4], [1,4,2],
5858
### **Python3**
5959

6060
```python
61-
61+
class Solution:
62+
def gardenNoAdj(self, n: int, paths: List[List[int]]) -> List[int]:
63+
g = defaultdict(list)
64+
for x, y in paths:
65+
x, y = x - 1, y - 1
66+
g[x].append(y)
67+
g[y].append(x)
68+
ans = [0] * n
69+
for u in range(n):
70+
colors = set(ans[v] for v in g[u])
71+
for c in range(1, 5):
72+
if c not in colors:
73+
ans[u] = c
74+
break
75+
return ans
6276
```
6377

6478
### **Java**
6579

6680
```java
81+
class Solution {
82+
public int[] gardenNoAdj(int n, int[][] paths) {
83+
List<Integer>[] g = new List[n];
84+
for (int i = 0; i < n; ++i) {
85+
g[i] = new ArrayList<>();
86+
}
87+
for (int[] p : paths) {
88+
int x = p[0] - 1, y = p[1] - 1;
89+
g[x].add(y);
90+
g[y].add(x);
91+
}
92+
int[] ans = new int[n];
93+
for (int u = 0; u < n; ++u) {
94+
Set<Integer> colors = new HashSet<>();
95+
for (int v : g[u]) {
96+
colors.add(ans[v]);
97+
}
98+
for (int c = 1; c < 5; ++c) {
99+
if (!colors.contains(c)) {
100+
ans[u] = c;
101+
break;
102+
}
103+
}
104+
}
105+
return ans;
106+
}
107+
}
108+
```
109+
110+
### **C++**
111+
112+
```cpp
113+
class Solution {
114+
public:
115+
vector<int> gardenNoAdj(int n, vector<vector<int>>& paths) {
116+
vector<vector<int>> g(n);
117+
for (auto& p : paths)
118+
{
119+
int x = p[0] - 1, y = p[1] - 1;
120+
g[x].push_back(y);
121+
g[y].push_back(x);
122+
}
123+
vector<int> ans(n);
124+
for (int u = 0; u < n; ++u)
125+
{
126+
unordered_set<int> colors;
127+
for (int v : g[u]) colors.insert(ans[v]);
128+
for (int c = 1; c < 5; ++c)
129+
{
130+
if (!colors.count(c))
131+
{
132+
ans[u] = c;
133+
break;
134+
}
135+
}
136+
}
137+
return ans;
138+
}
139+
};
140+
```
67141
142+
### **Go**
143+
144+
```go
145+
func gardenNoAdj(n int, paths [][]int) []int {
146+
g := make([][]int, n)
147+
for _, p := range paths {
148+
x, y := p[0]-1, p[1]-1
149+
g[x] = append(g[x], y)
150+
g[y] = append(g[y], x)
151+
}
152+
ans := make([]int, n)
153+
for u := 0; u < n; u++ {
154+
colors := make(map[int]bool)
155+
for _, v := range g[u] {
156+
colors[ans[v]] = true
157+
}
158+
for c := 1; c < 5; c++ {
159+
if !colors[c] {
160+
ans[u] = c
161+
break
162+
}
163+
}
164+
}
165+
return ans
166+
}
68167
```
69168

70169
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public:
3+
vector<int> gardenNoAdj(int n, vector<vector<int>>& paths) {
4+
vector<vector<int>> g(n);
5+
for (auto& p : paths)
6+
{
7+
int x = p[0] - 1, y = p[1] - 1;
8+
g[x].push_back(y);
9+
g[y].push_back(x);
10+
}
11+
vector<int> ans(n);
12+
for (int u = 0; u < n; ++u)
13+
{
14+
unordered_set<int> colors;
15+
for (int v : g[u]) colors.insert(ans[v]);
16+
for (int c = 1; c < 5; ++c)
17+
{
18+
if (!colors.count(c))
19+
{
20+
ans[u] = c;
21+
break;
22+
}
23+
}
24+
}
25+
return ans;
26+
}
27+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
func gardenNoAdj(n int, paths [][]int) []int {
2+
g := make([][]int, n)
3+
for _, p := range paths {
4+
x, y := p[0]-1, p[1]-1
5+
g[x] = append(g[x], y)
6+
g[y] = append(g[y], x)
7+
}
8+
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
13+
}
14+
for c := 1; c < 5; c++ {
15+
if !colors[c] {
16+
ans[u] = c
17+
break
18+
}
19+
}
20+
}
21+
return ans
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public int[] gardenNoAdj(int n, int[][] paths) {
3+
List<Integer>[] g = new List[n];
4+
for (int i = 0; i < n; ++i) {
5+
g[i] = new ArrayList<>();
6+
}
7+
for (int[] p : paths) {
8+
int x = p[0] - 1, y = p[1] - 1;
9+
g[x].add(y);
10+
g[y].add(x);
11+
}
12+
int[] ans = new int[n];
13+
for (int u = 0; u < n; ++u) {
14+
Set<Integer> colors = new HashSet<>();
15+
for (int v : g[u]) {
16+
colors.add(ans[v]);
17+
}
18+
for (int c = 1; c < 5; ++c) {
19+
if (!colors.contains(c)) {
20+
ans[u] = c;
21+
break;
22+
}
23+
}
24+
}
25+
return ans;
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def gardenNoAdj(self, n: int, paths: List[List[int]]) -> List[int]:
3+
g = defaultdict(list)
4+
for x, y in paths:
5+
x, y = x - 1, y - 1
6+
g[x].append(y)
7+
g[y].append(x)
8+
ans = [0] * n
9+
for u in range(n):
10+
colors = set(ans[v] for v in g[u])
11+
for c in range(1, 5):
12+
if c not in colors:
13+
ans[u] = c
14+
break
15+
return ans

0 commit comments

Comments
 (0)