-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathNumberOfProvinces.java
73 lines (66 loc) · 2.19 KB
/
NumberOfProvinces.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
/*https://practice.geeksforgeeks.org/problems/number-of-provinces/1*/
/*https://leetcode.com/problems/number-of-provinces/*/
//simple BFS
//adj is not adjacency list, it is adjacency matrix
class Solution {
static boolean[] visited;
static int numProvinces(ArrayList<ArrayList<Integer>> adj, int V) {
// code here
visited = new boolean[V];
int counter = 0, currNode = -1, j = 0;
Queue<Integer> queue = new LinkedList<Integer>();
for (int i = 0; i < V; ++i)
{
if (!visited[i])
{
++counter;
queue.add(i);
while (!queue.isEmpty())
{
currNode = queue.poll();
if (visited[currNode]) continue;
visited[currNode] = true;
for (j = 0; j < V; ++j)
{
if (currNode != j && (Integer)adj.get(currNode).get(j) == 1 && !visited[j])
queue.add(j);
}
}
}
}
return counter;
}
};
//disjoint set solution
class Solution {
HashSet<Integer> set;
public int findCircleNum(int[][] isConnected) {
int i, j, V = isConnected.length;
int[] parent = new int[V];
set = new HashSet<Integer>();
for (i = 0; i < V; ++i)
{
parent[i] = i;
set.add(i);
}
for (i = 0; i < V; ++i)
for (j = 0; j < V; ++j)
if (i != j && isConnected[i][j] == 1)
unionAndFind(parent, i, j);
return set.size(); //return the remaining number of parents
}
public void unionAndFind(int[] parent, int x, int y)
{
int parentX = find(parent,x);
int parentY = find(parent,y);
if (parentX == parentY) return;
parent[parentX] = parentY; //if parent is changing
set.remove(parentX); //remove the old parent from the set
}
public int find(int[] parent, int node)
{
if (parent[node] == node) return node;
parent[node] = find(parent,parent[node]);
return parent[node];
}
}