Skip to content

Commit 53e2275

Browse files
add a solution for 547
1 parent 411341e commit 53e2275

2 files changed

Lines changed: 78 additions & 24 deletions

File tree

  • src
    • main/java/com/fishercoder/solutions/firstthousand
    • test/java/com/fishercoder/firstthousand

‎src/main/java/com/fishercoder/solutions/firstthousand/_547.java‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package com.fishercoder.solutions.firstthousand;
22

3+
import java.util.ArrayList;
4+
import java.util.LinkedList;
5+
import java.util.List;
6+
import java.util.Queue;
7+
38
public class _547 {
49

510
public static class Solution1 {
@@ -51,4 +56,48 @@ public int find(int[] ids, int i) {
5156
}
5257
}
5358
}
59+
60+
public static class Solution2 {
61+
/**
62+
* My original solution:
63+
* The tricky part of this problem is you need to figure out the input, it's using i and j to denote vertex A and vertex B, then the 3rd value in each array denotes if there's an edge in between A and B.
64+
* Draw it out on a whiteboard to visualize this input, it'll make sense immediately.
65+
* Then you can apply the template of using BFS to traverse an un-directed graph to find all connected components.
66+
*/
67+
public int findCircleNum(int[][] isConnected) {
68+
List<List<Integer>> graph = new ArrayList<>();
69+
int m = isConnected.length;
70+
for (int i = 0; i < m; i++) {
71+
graph.add(new ArrayList<>());
72+
}
73+
for (int i = 0; i < m; i++) {
74+
for (int j = 0; j < m; j++) {
75+
if (i != j && isConnected[i][j] == 1) {
76+
graph.get(i).add(j);
77+
graph.get(j).add(i);
78+
}
79+
}
80+
}
81+
boolean[] visited = new boolean[m];
82+
int count = 0;
83+
for (int i = 0; i < m; i++) {
84+
if (!visited[i]) {
85+
count++;
86+
Queue<Integer> queue = new LinkedList<>();
87+
queue.add(i);
88+
while (!queue.isEmpty()) {
89+
int cur = queue.poll();
90+
visited[cur] = true;
91+
for (int next : graph.get(cur)) {
92+
if (!visited[next]) {
93+
visited[next] = true;
94+
queue.add(next);
95+
}
96+
}
97+
}
98+
}
99+
}
100+
return count;
101+
}
102+
}
54103
}

‎src/test/java/com/fishercoder/firstthousand/_547Test.java‎

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,68 +6,73 @@
66
import org.junit.jupiter.api.BeforeEach;
77
import org.junit.jupiter.api.Test;
88

9-
/** Created by fishercoder on 1/9/17. */
9+
/**
10+
* Created by fishercoder on 1/9/17.
11+
*/
1012
public class _547Test {
11-
private _547.Solution1 test;
13+
private _547.Solution1 solution1;
14+
private _547.Solution2 solution2;
1215
private static int expected;
1316
private static int actual;
1417
private static int[][] isConnected;
1518

1619
@BeforeEach
1720
public void setup() {
18-
test = new _547.Solution1();
21+
solution1 = new _547.Solution1();
22+
solution2 = new _547.Solution2();
1923
}
2024

2125
@Test
2226
public void test1() {
2327
isConnected =
24-
new int[][] {
25-
{1, 1, 0},
26-
{1, 1, 0},
27-
{0, 0, 1},
28+
new int[][]{
29+
{1, 1, 0},
30+
{1, 1, 0},
31+
{0, 0, 1},
2832
};
2933
expected = 2;
30-
actual = test.findCircleNum(isConnected);
34+
actual = solution1.findCircleNum(isConnected);
35+
actual = solution2.findCircleNum(isConnected);
3136
assertEquals(expected, actual);
3237
}
3338

3439
@Test
3540
public void test2() {
3641
isConnected =
37-
new int[][] {
38-
{1, 1, 0},
39-
{1, 1, 1},
40-
{0, 1, 1},
42+
new int[][]{
43+
{1, 1, 0},
44+
{1, 1, 1},
45+
{0, 1, 1},
4146
};
4247
expected = 1;
43-
actual = test.findCircleNum(isConnected);
48+
actual = solution1.findCircleNum(isConnected);
4449
assertEquals(expected, actual);
4550
}
4651

4752
@Test
4853
public void test3() {
4954
isConnected =
50-
new int[][] {
51-
{1, 0, 0, 1},
52-
{0, 1, 1, 0},
53-
{0, 1, 1, 1},
54-
{1, 0, 1, 1},
55+
new int[][]{
56+
{1, 0, 0, 1},
57+
{0, 1, 1, 0},
58+
{0, 1, 1, 1},
59+
{1, 0, 1, 1},
5560
};
5661
expected = 1;
57-
actual = test.findCircleNum(isConnected);
62+
actual = solution1.findCircleNum(isConnected);
5863
assertEquals(expected, actual);
5964
}
6065

6166
@Test
6267
public void test4() {
6368
isConnected =
64-
new int[][] {
65-
{1, 0, 0},
66-
{0, 1, 0},
67-
{0, 0, 1},
69+
new int[][]{
70+
{1, 0, 0},
71+
{0, 1, 0},
72+
{0, 0, 1},
6873
};
6974
expected = 3;
70-
actual = test.findCircleNum(isConnected);
75+
actual = solution1.findCircleNum(isConnected);
7176
assertEquals(expected, actual);
7277
}
7378
}

0 commit comments

Comments
 (0)