Skip to content

Instantly share code, notes, and snippets.

@wushbin
Created March 24, 2020 18:23
Show Gist options
  • Select an option

  • Save wushbin/1bddcea1be95e5aea323afba88c2748a to your computer and use it in GitHub Desktop.

Select an option

Save wushbin/1bddcea1be95e5aea323afba88c2748a to your computer and use it in GitHub Desktop.
class Solution {
public int[] gardenNoAdj(int N, int[][] paths) {
Map<Integer, Set<Integer>> graph = new HashMap<>();
for (int i = 1; i <= N; i++) {
graph.put(i, new HashSet<>());
}
for (int[] path : paths) {
graph.get(path[0]).add(path[1]);
graph.get(path[1]).add(path[0]);
}
int[] result = new int[N];
for (int i = 1; i <= N; i++) {
int[] color = new int[5];
for (int neighbor : graph.get(i)) {
color[result[neighbor - 1]] += 1;
}
for (int j = 4; j > 0; --j) {
if (color[j] == 0) {
result[i - 1] = j;
}
}
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment