Skip to content

Commit 3b5d091

Browse files
add 1557
1 parent 1a937be commit 3b5d091

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1557|[Minimum Number of Vertices to Reach All Nodes](https://leetcode.com/problems/minimum-number-of-vertices-to-reach-all-nodes/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1557.java) ||Medium|Graph|
1112
|1556|[Thousand Separator](https://leetcode.com/problems/thousand-separator/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1556.java) ||Easy|String|
1213
|1551|[Minimum Operations to Make Array Equal](https://leetcode.com/problems/minimum-operations-to-make-array-equal/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1551.java) | [:tv:](https://youtu.be/A-i2sxmBqAA)|Medium|Math|
1314
|1550|[Three Consecutive Odds](https://leetcode.com/problems/three-consecutive-odds/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1550.java) | |Easy|Array|
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.*;
4+
5+
public class _1557 {
6+
public static class Solution1 {
7+
public List<Integer> findSmallestSetOfVertices(int n, List<List<Integer>> edges) {
8+
List<Integer> starts = new ArrayList<>();
9+
Map<Integer, Set<Integer>> indegree = new HashMap<>();
10+
for (int i = 0; i < edges.size(); i++) {
11+
int end = edges.get(i).get(1);
12+
if (!indegree.containsKey(end)) {
13+
indegree.put(end, new HashSet<>());
14+
}
15+
indegree.get(end).add(edges.get(i).get(0));
16+
}
17+
for (int i = 0; i < n; i++) {
18+
if (!indegree.containsKey(i)) {
19+
starts.add(i);
20+
}
21+
}
22+
return starts;
23+
}
24+
}
25+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1557;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import java.util.Arrays;
8+
import java.util.List;
9+
10+
import static org.junit.Assert.assertEquals;
11+
12+
public class _1557Test {
13+
private static _1557.Solution1 solution1;
14+
private static List<List<Integer>> edges;
15+
16+
@BeforeClass
17+
public static void setup() {
18+
solution1 = new _1557.Solution1();
19+
}
20+
21+
@Test
22+
public void test1() {
23+
edges = Arrays.asList(Arrays.asList(0, 1), Arrays.asList(0, 2), Arrays.asList(2, 5), Arrays.asList(3, 4), Arrays.asList(4, 2));
24+
assertEquals(Arrays.asList(0, 3), solution1.findSmallestSetOfVertices(6, edges));
25+
}
26+
27+
@Test
28+
public void test2() {
29+
edges = Arrays.asList(Arrays.asList(0, 1), Arrays.asList(2, 1), Arrays.asList(3, 1), Arrays.asList(1, 4), Arrays.asList(2, 4));
30+
assertEquals(Arrays.asList(0, 2, 3), solution1.findSmallestSetOfVertices(5, edges));
31+
}
32+
33+
}

0 commit comments

Comments
 (0)