Skip to content

Commit c0d0a91

Browse files
committed
added problem 1557
1 parent 3332ef2 commit c0d0a91

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

problems/problem1557/min_verts.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package problem1557
2+
3+
/*
4+
Given a directed acyclic graph, with n vertices numbered from 0 to n-1,
5+
and an array edges where edges[i] = [fromi, toi] represents a directed edge from node fromi to node toi.
6+
Find the smallest set of vertices from which all nodes in the graph are reachable.
7+
It's guaranteed that a unique solution exists.
8+
Notice that you can return the vertices in any order.
9+
*/
10+
11+
func findSmallestSetOfVertices(n int, edges [][]int) []int {
12+
var verts = make([]bool, n)
13+
var res = make([]int, 0, n)
14+
// Loop over edges to see which ones don't
15+
// have incoming arrows
16+
for _, e := range edges {
17+
verts[e[1]] = true
18+
}
19+
// Add verts with no incoming arrows
20+
for i := range verts {
21+
if !verts[i] {
22+
res = append(res, i)
23+
}
24+
}
25+
return res
26+
}
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package problem1557
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
type Result struct {
11+
N int
12+
Input [][]int
13+
Expected []int
14+
}
15+
16+
var Results = []Result{
17+
{6, [][]int{{0, 1}, {0, 2}, {2, 5}, {3, 4}, {4, 2}}, []int{0, 3}},
18+
{5, [][]int{{0, 1}, {2, 1}, {3, 1}, {1, 4}, {2, 4}}, []int{0, 2, 3}},
19+
}
20+
21+
func TestMinVertsToReachAll(t *testing.T) {
22+
assert := assert.New(t)
23+
24+
for _, res := range Results {
25+
want := res.Expected
26+
got := findSmallestSetOfVertices(res.N, res.Input)
27+
assert.Equal(want, got, fmt.Sprintf("%+v", res))
28+
}
29+
}
30+
31+
func BenchmarkMinVertsToReachAll(b *testing.B) {
32+
for i := 0; i < b.N; i++ {
33+
for _, res := range Results {
34+
findSmallestSetOfVertices(res.N, res.Input)
35+
}
36+
}
37+
}

readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,7 @@ Each problem is in it's own directory, with test files. There are helper package
402402
| 1523 | [Count Odd Numbers in an Interval Range](https://leetcode.com/problems/count-odd-numbers-in-an-interval-range) | [My Solution](./problems/problem1523) ||
403403
| 1539 | [Kth Missing Positive Number](https://leetcode.com/problems/kth-missing-positive-number) | [My Solution](./problems/problem1539) ||
404404
| 1544 | [Make The String Great](https://leetcode.com/problems/make-the-string-great) | [My Solution](./problems/problem1544) ||
405+
| 1557 | [Minimum Number of Vertices to Reach All Nodes](https://leetcode.com/problems/minimum-number-of-vertices-to-reach-all-nodes) | [My Solution](./problems/problem1557) ||
405406
| 1572 | [Matrix Diagonal Sum](https://leetcode.com/problems/matrix-diagonal-sum) | [My Solution](./problems/problem1572) ||
406407
| 1578 | [Minimum Time to Make Rope Colorful](https://leetcode.com/problems/minimum-time-to-make-rope-colorful) | [My Solution](./problems/problem1578) ||
407408
| 1579 | [Remove Max Number of Edges to Keep Graph Fully Traversable](https://leetcode.com/problems/remove-max-number-of-edges-to-keep-graph-fully-traversable) | [My Solution](./problems/problem1579) ||

0 commit comments

Comments
 (0)