|
| 1 | +In a town, there are `N` people labelled from `1` to `N`. There is a rumor that one of these people is secretly the town judge. |
| 2 | + |
| 3 | +If the town judge exists, then: |
| 4 | + |
| 5 | +1. The town judge trusts nobody. |
| 6 | +2. Everybody (except for the town judge) trusts the town judge. |
| 7 | +3. There is exactly one person that satisfies properties 1 and 2. |
| 8 | + |
| 9 | +You are given `trust`, an array of pairs `trust[i] = [a, b]` representing that the person labelled `a` trusts the person labelled `b`. |
| 10 | + |
| 11 | +If the town judge exists and can be identified, return the label of the town judge. Otherwise, return `-1`. |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +## Example 1: |
| 16 | +``` |
| 17 | +Input: N = 2, trust = [[1,2]] |
| 18 | +Output: 2 |
| 19 | +``` |
| 20 | +## Example 2: |
| 21 | +``` |
| 22 | +Input: N = 3, trust = [[1,3],[2,3]] |
| 23 | +Output: 3 |
| 24 | +``` |
| 25 | +## Example 3: |
| 26 | +``` |
| 27 | +Input: N = 3, trust = [[1,3],[2,3],[3,1]] |
| 28 | +Output: -1 |
| 29 | +``` |
| 30 | +## Example 4: |
| 31 | +``` |
| 32 | +Input: N = 3, trust = [[1,2],[2,3]] |
| 33 | +Output: -1 |
| 34 | +``` |
| 35 | +## Example 5: |
| 36 | +``` |
| 37 | +Input: N = 4, trust = [[1,3],[1,4],[2,3],[2,4],[4,3]] |
| 38 | +Output: 3 |
| 39 | +``` |
| 40 | + |
| 41 | +## Constraints: |
| 42 | + |
| 43 | +* `1 <= N <= 1000` |
| 44 | +* `0 <= trust.length <= 10^4` |
| 45 | +* `trust[i].length == 2` |
| 46 | +* `trust[i] are all different` |
| 47 | +* `trust[i][0] != trust[i][1]` |
| 48 | +* `1 <= trust[i][0], trust[i][1] <= N` |
0 commit comments