Created
March 24, 2020 18:23
-
-
Save wushbin/1bddcea1be95e5aea323afba88c2748a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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