package leetcode;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * https://leetcode.com/problems/flower-planting-with-no-adjacent/
 */
public class Problem1042 {
    public int[] gardenNoAdj(int N, int[][] paths) {
        Set<Integer>[] set = new HashSet[N + 1];
        for (int i = 1; i <= N; i++) {
            set[i] = new HashSet<>();
        }
        for (int i = 1; i <= N; i++) {
            int a = paths[i][0];
            int b = paths[i][1];
            set[a].add(b);
            set[b].add(a);
        }
        int[] answer = new int[N];
        for (int i = 1; i <= N; i++) {
            // TODO
        }
        return answer;
    }

    public static void main(String[] args) {
        Problem1042 prob = new Problem1042();
        System.out.println(Arrays.toString(prob.gardenNoAdj(3, new int[][]{{1, 2}, {2, 3}, {3, 1}}))); // [1,2,3]
//        System.out.println(Arrays.toString(prob.gardenNoAdj(4, new int[][]{{1, 2}, {3, 4}}))); // [1,2,1,2]
//        System.out.println(Arrays.toString(prob.gardenNoAdj(4, new int[][]{{1, 2}, {2, 3}, {3, 4}, {4, 1}, {1, 3}, {2, 4}}))); // [1,2,3,4]
    }
}

@@ -16,33 +16,8 @@
 e;%0A%0A
-import java.util.Arrays;%0A
 impo
@@ -368,35 +368,31 @@
 for (int
- i = 1; i %3C= N; i++
+%5B%5D path : paths
 ) %7B%0A    
@@ -411,20 +411,16 @@
 a = path
-s%5Bi%5D
 %5B0%5D;%0A   
@@ -444,12 +444,8 @@
 path
-s%5Bi%5D
 %5B1%5D;
@@ -556,68 +556,36 @@
     
-for (int i = 1; i %3C= N; i++) %7B%0A            // TODO%0A        %7D
+gardenNoAdj(answer, set, 0);
 %0A   
@@ -620,21 +620,22 @@
    p
-ublic
+rivate
  static 
 void
@@ -634,446 +634,662 @@
 tic 
-void main(String
+boolean gardenNoAdj(int
 %5B%5D a
-rgs) %7B%0A        Problem1042 prob = new Problem1042();%0A        System.out.println(Arrays.toString(prob.gardenNoAdj(3, new int%5B%5D%5B%5D%7B%7B1, 2%7D, %7B2, 3%7D, %7B3, 1%7D%7D))); // %5B1,2,3%5D%0A//        System.out.println(Arrays.toString(prob.gardenNoAdj(4, new int%5B%5D%5B%5D%7B%7B1, 2%7D, %7B3, 4%7D%7D))); // %5B1,2,1,2%5D%0A//        System.out.println(Arrays.toString(prob.gardenNoAdj(4, new int%5B%5D%5B%5D%7B%7B1, 2%7D, %7B2, 3%7D, %7B3, 4%7D, %7B4, 1%7D, %7B1, 3%7D, %7B2, 4%7D%7D))); // %5B1,2,3,4%5D
+nswer, Set%3CInteger%3E%5B%5D set, int index) %7B%0A        if (index == answer.length) %7B%0A            return true;%0A        %7D%0A        for (int i = 1; i %3C= 4; i++) %7B%0A            answer%5Bindex%5D = i;%0A            if (isValid(answer, set, index) && gardenNoAdj(answer, set, index + 1)) %7B%0A                break;%0A            %7D%0A        %7D%0A        return true;%0A    %7D%0A%0A    private static boolean isValid(int%5B%5D answer, Set%3CInteger%3E%5B%5D set, int index) %7B%0A        Set%3CInteger%3E adjs = set%5Bindex + 1%5D;%0A        for (int adj : adjs) %7B%0A            if (answer%5Bindex%5D == answer%5Badj - 1%5D) %7B%0A                return false;%0A            %7D%0A        %7D%0A        return true;
 %0A   

Solve problem 1042