-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution547.java
More file actions
39 lines (29 loc) · 1.05 KB
/
Copy pathSolution547.java
File metadata and controls
39 lines (29 loc) · 1.05 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
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class Solution547 {
public int findCircleNum(int[][] isConnected) {
ArrayList<Integer> remainingCities = new ArrayList<>();
for (int i = 0; i < isConnected.length; i++) {
remainingCities.add(i);
}
return provincesNumber(isConnected, remainingCities);
}
private int provincesNumber(int[][] table, List<Integer> remainingCities) {
List<Integer> cities = connectedCities(table, remainingCities.get(0));
remainingCities.removeAll(cities);
if (!remainingCities.isEmpty()) {
return 1 + provincesNumber(table, remainingCities);
}
return 1;
}
private List<Integer> connectedCities(int[][] table, int city) {
LinkedList<Integer> connected = new LinkedList<>();
for (int i = 0; i < table.length; i++) {
if (table[i][city] == 1) connected.add(i);
}
return connected;
}
public static void main(String[] args) {
}
}