Skip to content
LC-1557 Medium LeetCode

1557. Minimum Number of Vertices to Reach All Nodes

Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 81% Topics: Graph
View full problem on LeetCode

Reading material

Reference solution (spoiler · python)
# Time:  O(e)
# Space: O(n)

class Solution(object):
    def findSmallestSetOfVertices(self, n, edges):
        """
        :type n: int
        :type edges: List[List[int]]
        :rtype: List[int]
        """
        result = []
        lookup = set()
        for u, v in edges:
            lookup.add(v)
        for i in xrange(n):
            if i not in lookup:
                result.append(i)
        return result

Solution from kamyu104/LeetCode-Solutions · MIT