-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy path1129-shortest-path-with-alternating-colors.java
57 lines (54 loc) · 1.93 KB
/
1129-shortest-path-with-alternating-colors.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
class Solution {
public int[] shortestAlternatingPaths(int n, int[][] redEdges, int[][] blueEdges) {
Map<Integer, List<Integer>> red = new HashMap<>();
Map<Integer, List<Integer>> blue = new HashMap<>();
for (int i = 0; i < n; i++) {
red.put(i, new ArrayList<>());
blue.put(i, new ArrayList<>());
}
for (int[] redEdge : redEdges) {
red.get(redEdge[0]).add(redEdge[1]);
}
for (int[] blueEdge : blueEdges) {
blue.get(blueEdge[0]).add(blueEdge[1]);
}
int[] answer = new int[n];
Arrays.fill(answer, -1);
ArrayDeque<ColorStep> deque = new ArrayDeque<>();
boolean[][] visit = new boolean[n][2];
deque.addLast(new ColorStep(0, 0, -1));
while (!deque.isEmpty()) {
ColorStep step = deque.pollFirst();
if (answer[step.node] == -1) {
answer[step.node] = step.length;
}
if (step.prevEdgeColor != 0) {
for (int nei : red.get(step.node)) {
if (!visit[nei][0]) {
visit[nei][0] = true;
deque.addLast(new ColorStep(nei, step.length + 1, 0));
}
}
}
if (step.prevEdgeColor != 1) {
for (int nei : blue.get(step.node)) {
if (!visit[nei][1]) {
visit[nei][1] = true;
deque.addLast(new ColorStep(nei, step.length + 1, 1));
}
}
}
}
return answer;
}
public class ColorStep {
int node;
int length;
int prevEdgeColor; // 0 is for Red, 1 is for Blue
public ColorStep(int node, int length, int prevEdgeColor) {
this.node = node;
this.length = length;
this.prevEdgeColor = prevEdgeColor;
}
}
}