Network Endpoint or Cycle Boundary
Problem statement
You are given a directed network with n nodes numbered from 0 to n - 1. Each pair [from, to] in edges creates one directed edge from from to to. Every node has at most one outgoing edge.
Start at start and repeatedly follow the current node's outgoing edge.
- If the current node has no outgoing edge, return that endpoint.
- If following the next edge would revisit any node already seen on this walk, return the current node. It is the last node visited before the walk would repeat.
Function
findNetworkResult(n: int, edges: int[][], start: int) → intExamples
Example 1
n = 10edges = [[1,3],[7,3],[3,4],[4,6],[2,6],[6,9],[9,5]]start = 1return = 5The walk is 1 → 3 → 4 → 6 → 9 → 5. Node 5 has no outgoing edge.
Example 2
n = 5edges = [[0,1],[1,2],[2,3],[3,1]]start = 0return = 3The next edge from node 3 leads to the already visited node 1, so return 3.
Example 3
n = 6edges = [[0,2],[2,4]]start = 5return = 5The starting node already has no outgoing edge.
Constraints
1 ≤ n ≤ 2000000 ≤ edges.length ≤ n- Every edge contains two node labels in
[0, n - 1]. - No node appears as
frommore than once. 0 ≤ start < n