Repository navigation
Expand file tree
/
Copy pathEscapePods.java
More file actions
71 lines (64 loc) · 2.07 KB
/
Copy pathEscapePods.java
File metadata and controls
71 lines (64 loc) · 2.07 KB
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
package google;
import java.util.*;
/**
* Why Did you create this class? what does it do?
*/
@SuppressWarnings("Duplicates")
public class EscapePods {
public static void main(String[] args) {
System.out.println(answer(new int[] { 0, 1 }, new int[] { 4, 5 }, new int[][] {
{ 0, 0, 4, 6, 0, 0 },
{ 0, 0, 5, 2, 0, 0 },
{ 0, 0, 0, 0, 4, 4 },
{ 0, 0, 0, 0, 6, 6 },
{ 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0 }
}));
System.out.println(answer(new int[] { 0 }, new int[] { 3 }, new int[][] {
{ 0, 7, 0, 0 },
{ 0, 0, 6, 0 },
{ 0, 0, 0, 8 },
{ 9, 0, 0, 0 },
}));
}
public static int answer(int[] entrances, int[] exits, int[][] path) {
if (entrances == null || entrances.length == 0 || exits == null || exits.length == 0)
return 0;
if (path == null || path.length == 0)
return 0;
int res = 0;
int[] rooms = new int[50];
Queue<Integer> q = new LinkedList<>();
Set<Integer> setE = new HashSet<>();
for (int i = 0; i < entrances.length; i++) {
rooms[entrances[i]] = -1;
q.add(entrances[i]);
setE.add(entrances[i]);
}
for (int exit : exits) {
setE.add(exit);
}
while (!q.isEmpty()) {
int room = q.remove();
setE.remove(room);
int[] nei = path[room];
for (int i = 0; i < nei.length; i++) {
if (nei[i] == 0)
continue;
int val = nei[i];
if (rooms[room] != -1) {
val = Math.min(val, rooms[room]);
rooms[room] -= val;
}
rooms[i] += val;
if (setE.add(i)) {
q.add(i);
}
}
}
for (int i = 0; i < exits.length; i++) {
res += rooms[exits[i]];
}
return res;
}
}