We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 1965aab commit 8d50adaCopy full SHA for 8d50ada
Easy/Flower Planting With No Adjacent.java
@@ -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