Skip to content

Commit 69c6f60

Browse files
add a solution for 684
1 parent 83d657a commit 69c6f60

File tree

3 files changed

+48
-6
lines changed
  • paginated_contents/algorithms/1st_thousand
  • src
    • main/java/com/fishercoder/solutions/firstthousand
    • test/java/com/fishercoder/firstthousand

3 files changed

+48
-6
lines changed

Diff for: paginated_contents/algorithms/1st_thousand/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@
186186
| 687 | [Longest Univalue Path](https://leetcode.com/problems/longest-univalue-path/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_687.java) | | Easy | DFS
187187
| 686 | [Repeated String Match](https://leetcode.com/problems/repeated-string-match/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_686.java) | | Easy |
188188
| 685 | [Redundant Connection II](https://leetcode.com/problems/redundant-connection-ii/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_685.java) | | Hard | Union Find
189-
| 684 | [Redundant Connection](https://leetcode.com/problems/redundant-connection/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_684.java) | | Medium | Union Find
189+
| 684 | [Redundant Connection](https://leetcode.com/problems/redundant-connection/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_684.java) | | Medium | Union Find, DFS
190190
| 683 | [K Empty Slots](https://leetcode.com/problems/k-empty-slots/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_683.java) | | Hard |
191191
| 682 | [Baseball Game](https://leetcode.com/problems/baseball-game/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_682.java) | | Easy |
192192
| 681 | [Next Closest Time](https://leetcode.com/problems/parents-closest-time/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/firstthousand/_681.java) | | Medium |

Diff for: src/main/java/com/fishercoder/solutions/firstthousand/_684.java

+42
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.fishercoder.solutions.firstthousand;
22

3+
import java.util.ArrayList;
34
import java.util.HashSet;
5+
import java.util.List;
46
import java.util.Set;
57

68
public class _684 {
@@ -83,4 +85,44 @@ public int[] findRedundantConnection(int[][] edges) {
8385
return result;
8486
}
8587
}
88+
89+
public static class Solution2 {
90+
/**
91+
* DFS, credit: https://leetcode.com/problems/redundant-connection/editorial/
92+
* 1. we build the graph one edge at a time, each time, we add both edge[0] to the neighbors of edge[1] and vice versa since this is an un-directed graph;
93+
* 2. as soon as we encounter an edge that can connect to each other, it must be the redundant one.
94+
*/
95+
private static final int MAX_VERTICES = 1000;
96+
97+
public int[] findRedundantConnection(int[][] edges) {
98+
List<Integer>[] graph = new ArrayList[MAX_VERTICES + 1];
99+
for (int i = 0; i < graph.length; i++) {
100+
graph[i] = new ArrayList<>();
101+
}
102+
Set<Integer> visited = new HashSet<>();
103+
for (int[] edge : edges) {
104+
visited.clear();
105+
if (!graph[edge[0]].isEmpty() && !graph[edge[1]].isEmpty() && canConnect(edge[0], edge[1], graph, visited)) {
106+
return edge;
107+
}
108+
graph[edge[0]].add(edge[1]);
109+
graph[edge[1]].add(edge[0]);
110+
}
111+
return null;
112+
}
113+
114+
private boolean canConnect(int source, int target, List<Integer>[] graph, Set<Integer> visited) {
115+
if (visited.add(source)) {
116+
if (source == target) {
117+
return true;
118+
}
119+
for (int v : graph[target]) {
120+
if (canConnect(source, v, graph, visited)) {
121+
return true;
122+
}
123+
}
124+
}
125+
return false;
126+
}
127+
}
86128
}

Diff for: src/test/java/com/fishercoder/firstthousand/_684Test.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
package com.fishercoder.firstthousand;
22

33
import com.fishercoder.solutions.firstthousand._684;
4-
import org.junit.BeforeClass;
5-
import org.junit.Test;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
66

7-
import static org.junit.Assert.assertArrayEquals;
7+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
88

99
public class _684Test {
1010
private static _684.Solution1 solution1;
1111
private static int[][] edges;
1212
private static int[] expected;
1313

14-
@BeforeClass
15-
public static void setup() {
14+
@BeforeEach
15+
public void setup() {
1616
solution1 = new _684.Solution1();
1717
}
1818

0 commit comments

Comments
 (0)