#997

Find the Town Judge

pupil · 440 · lc easy +26 · verified · 50.5% accepted · 6,952 likes · top 38%

Description

In a town of n people (labeled 1 to n), a town judge exists if:

- The town judge trusts nobody.

- Every other person trusts the town judge.

- Exactly one person meets both conditions.

You are given a trust array where trust[i] = [ai, bi] means person ai trusts person bi.

Return the label of the town judge if one can be identified, or -1 otherwise.

Example 1:

Input: n = 2, trust = [[1,2]]
Output: 2

Example 2:

Input: n = 3, trust = [[1,3],[2,3]]
Output: 3

Example 3:

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

Code

1
2
3