Skip to the code
cf.
+10 XP

#407 Find the Final Approver

easy

In a design review tool, people defer to each other's judgment. People are numbered 0 to n - 1, and defers[i] = [a, b] means person a defers to person b.

The final approver is the one person whom everybody else defers to, and who defers to nobody. Write findApprover(n, defers) that returns that person's number, or -1 if no such person exists.

No pair appears twice and nobody defers to themselves. With n = 1 and no pairs, person 0 is the final approver.

Example 1

Input: n = 3, defers = [[0,2],[1,2]]
Output: 2

Persons 0 and 1 both defer to 2, and 2 defers to nobody.

Example 2

Input: n = 3, defers = [[0,2],[1,2],[2,0]]
Output: -1

Person 2 defers to 0, so 2 is not the final approver, and nobody else is deferred to by everyone.

Constraints

  • 1 <= n <= 1000
  • 0 <= defers.length <= 10000
  • 0 <= a, b < n and a !== b
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).