FunctionRacerProblemsRace

Dead Ends

BeginnerGraphsPython, JavaScript, TypeScript, Java, C++

A directed graph has n nodes numbered 0 to n - 1. arrows is a list of pairs [a, b], each an arrow that leaves node a and points at node b. The same pair may appear more than once, and an arrow may point from a node back to itself.

A node is a dead end when no arrow leaves it. An arrow from a node to itself does leave that node, so such a node is not a dead end. Return the dead ends in increasing order. Return [] when every node has an arrow leaving it; when arrows is empty every node is a dead end.

Example 1

n 4
arrows [[0,1],[1,2],[3,3]]
returns [2]

Arrows leave nodes 0, 1 and 3 (the arrow [3, 3] leaves node 3), so only node 2 is a dead end.

Example 2

n 3
arrows [[0,1],[1,0]]
returns [2]

Nodes 0 and 1 point at each other; nothing leaves node 2.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Graphs problems