LeetCode Entry

1557. Minimum Number of Vertices to Reach All Nodes

18.05.2023 medium 2023 kotlin

Find all starting nodes in graph.

1557. Minimum Number of Vertices to Reach All Nodes medium blog post substack

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/216

Problem TLDR

Find all starting nodes in graph.

Intuition

Count nodes that have no incoming connections.

Approach

  • we can use subtract operation in Kotlin

    Complexity

  • Time complexity: \(O(n)\)
  • Space complexity: \(O(n)\)

Code


fun findSmallestSetOfVertices(n: Int, edges: List<List<Int>>): List<Int> =
    (0 until n) - edges.map { it[1] }