-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path1042.Flower-Planting-With-No-Adjacent.java
74 lines (63 loc) · 2 KB
/
1042.Flower-Planting-With-No-Adjacent.java
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// https://leetcode.com/problems/flower-planting-with-no-adjacent/
//
// algorithms
// Easy (38.68%)
// Total Accepted: 1,501
// Total Submissions: 3,881
// beats 100.0% of java submissions
class Solution {
public int[] gardenNoAdj(int N, int[][] paths) {
int[] res = new int[N];
HashMap<Integer, ArrayList<Integer>> map = new HashMap<>();
for (int i = 0; i < paths.length; i++) {
if (map.containsKey(paths[i][0])) {
map.get(paths[i][0]).add(paths[i][1]);
} else {
ArrayList<Integer> l = new ArrayList<>();
l.add(paths[i][1]);
map.put(paths[i][0], l);
}
if (map.containsKey(paths[i][1])) {
map.get(paths[i][1]).add(paths[i][0]);
} else {
ArrayList<Integer> l = new ArrayList<>();
l.add(paths[i][0]);
map.put(paths[i][1], l);
}
}
for (int i = 0; i < N; i++) {
if (res[i] == 0) {
recursive(map, res, i + 1);
if (res[i] == 0) {
res[i] = 1;
}
}
}
return res;
}
public void recursive(HashMap<Integer, ArrayList<Integer>> map, int[] res, int idx) {
if (!map.containsKey(idx)) {
return;
}
ArrayList<Integer> l = map.get(idx);
ArrayList<Integer> arr = new ArrayList<>();
boolean[] flag = new boolean[5];
Arrays.fill(flag, true);
for (int i : l) {
if (res[i - 1] != 0) {
flag[res[i - 1]] = false;
} else {
arr.add(i);
}
}
for (int i = 1; i < 5; i++) {
if (flag[i]) {
res[idx - 1] = i;
break;
}
}
for (int i : arr) {
recursive(map, res, i);
}
}
}