We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 6ecc8d3 + ba8fe5d commit 686a059Copy full SHA for 686a059
June2023/Java/NumberOfProvinces.java
@@ -0,0 +1,42 @@
1
+// @saorav21994
2
+
3
+class Solution {
4
+ public int findCircleNum(int[][] isConnected) {
5
6
+ // k-connected component == number of islands
7
8
+ int r = isConnected.length;
9
+ int c = r; // square matrix
10
11
+ Queue<Integer> queue = new LinkedList<>();
12
+ int [] hash = new int[r];
13
14
+ int province = 0;
15
16
+ for (int i = 0; i < r; i++) {
17
18
+ if (hash[i] == 0) {
19
20
+ province += 1;
21
22
+ queue.offer(i);
23
24
+ while (!queue.isEmpty()) {
25
+ int index = queue.poll();
26
+ if (hash[index] != 1) {
27
+ for (int j = 0; j < r; j++) {
28
+ if (isConnected[index][j] == 1 && index != j) {
29
+ queue.offer(j);
30
+ }
31
32
33
+ hash[index] = 1;
34
35
36
37
38
39
+ return province;
40
41
42
+}
0 commit comments