Skip to content

Commit 8d50ada

Browse files
committed
Added 1 solution
1 parent 1965aab commit 8d50ada

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public int[] gardenNoAdj(int N, int[][] paths) {
3+
Map<Integer, List<Integer>> map = new HashMap<>();
4+
for (int[] path : paths) {
5+
map.computeIfAbsent(path[0], k -> new ArrayList<>()).add(path[1]);
6+
map.computeIfAbsent(path[1], k -> new ArrayList<>()).add(path[0]);
7+
}
8+
int[] ans = new int[N];
9+
for (int i = 0; i < N; i++) {
10+
Set<Integer> neighbourPlant = new HashSet<>();
11+
for (Integer neighbour : map.getOrDefault(i + 1, new ArrayList<>())) {
12+
if (ans[neighbour - 1] != 0) {
13+
neighbourPlant.add(ans[neighbour - 1]);
14+
}
15+
}
16+
for (int j = 1; j <= 4; j++) {
17+
if (!neighbourPlant.contains(j)) {
18+
ans[i] = j;
19+
break;
20+
}
21+
}
22+
}
23+
return ans;
24+
}
25+
}

0 commit comments

Comments
 (0)