-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution1042.java
More file actions
30 lines (30 loc) · 986 Bytes
/
Copy pathSolution1042.java
File metadata and controls
30 lines (30 loc) · 986 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution1042 {
public int[] gardenNoAdj(int n, int[][] paths) {
List<List<Integer>> adjacent = new ArrayList<>(); // 邻接表
for(int i = 0; i < n; i++) {
adjacent.add(new ArrayList<>());
}
// 用邻接表表示图
for (int i = 0; i < paths.length; i++){
adjacent.get(paths[i][0] - 1).add(paths[i][1] - 1);
adjacent.get(paths[i][1] - 1).add(paths[i][0] - 1);
}
// 颜色标记法
int[] res = new int[n];
for (int i = 0; i < n; i++) {
// 统计周围节点的颜色
boolean[] colors = new boolean[5];
for (int adj : adjacent.get(i)) {
colors[res[adj]] = true;
}
// 给当前节点上色
for (int j = 1; j <= 4; j++) {
if (!colors[j]) {
res[i] = j;
break;
}
}
}
return res;
}
}