Skip to content
BytePatterns

Trusted Town Judge

EasyGraphs#degree-counting#directed-graph~15m

Problem

A town has n people labelled from 1 to n, and a list of pairs where the pair a, b means a trusts b. Exactly one person can be the judge: the judge trusts nobody, and every other person in town trusts the judge. Return the judge's label, or -1 when no such person exists.

Examples

Input:  n = 2, trust = [[1, 2]]
Output: 2
Why:    person 2 trusts nobody and is trusted by the only other person
Input:  n = 3, trust = [[1, 3], [2, 3], [3, 1]]
Output: -1
Why:    person 3 is trusted by everyone but trusts someone back
Input:  n = 1, trust = []
Output: 1
Why:    edge case, the only resident vacuously satisfies both rules

Hints

0 / 3

Stuck on the idea rather than the code? Graph Basics covers it.