Skip to content

Commit b11ebbf

Browse files
committed
Add solution #1557
1 parent 090d5f5 commit b11ebbf

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,365 LeetCode solutions in JavaScript
1+
# 1,366 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1188,6 +1188,7 @@
11881188
1552|[Magnetic Force Between Two Balls](./solutions/1552-magnetic-force-between-two-balls.js)|Medium|
11891189
1553|[Minimum Number of Days to Eat N Oranges](./solutions/1553-minimum-number-of-days-to-eat-n-oranges.js)|Hard|
11901190
1556|[Thousand Separator](./solutions/1556-thousand-separator.js)|Easy|
1191+
1557|[Minimum Number of Vertices to Reach All Nodes](./solutions/1557-minimum-number-of-vertices-to-reach-all-nodes.js)|Medium|
11911192
1566|[Detect Pattern of Length M Repeated K or More Times](./solutions/1566-detect-pattern-of-length-m-repeated-k-or-more-times.js)|Easy|
11921193
1576|[Replace All ?'s to Avoid Consecutive Repeating Characters](./solutions/1576-replace-all-s-to-avoid-consecutive-repeating-characters.js)|Medium|
11931194
1598|[Crawler Log Folder](./solutions/1598-crawler-log-folder.js)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* 1557. Minimum Number of Vertices to Reach All Nodes
3+
* https://leetcode.com/problems/minimum-number-of-vertices-to-reach-all-nodes/
4+
* Difficulty: Medium
5+
*
6+
* Given a directed acyclic graph, with n vertices numbered from 0 to n-1, and an array edges
7+
* where edges[i] = [fromi, toi] represents a directed edge from node fromi to node toi.
8+
*
9+
* Find the smallest set of vertices from which all nodes in the graph are reachable. It's
10+
* guaranteed that a unique solution exists.
11+
*
12+
* Notice that you can return the vertices in any order.
13+
*/
14+
15+
/**
16+
* @param {number} n
17+
* @param {number[][]} edges
18+
* @return {number[]}
19+
*/
20+
var findSmallestSetOfVertices = function(n, edges) {
21+
const hasIncomingEdge = new Array(n).fill(false);
22+
for (const [_, to] of edges) {
23+
hasIncomingEdge[to] = true;
24+
}
25+
26+
const result = [];
27+
for (let i = 0; i < n; i++) {
28+
if (!hasIncomingEdge[i]) {
29+
result.push(i);
30+
}
31+
}
32+
33+
return result;
34+
};

0 commit comments

Comments
 (0)