
😎풀이
- 아무도 믿지 않는 사람이 판사임
trust
를 순회하며 최대 지지를 받는 사람을 추림
- 최대 지지자는 반드시 본인을 제외한 모두에게 신뢰를 받아야 하며, 없다면 판사가 없는 것
- 모두의 지지를 받으며 본인은 아무도 신뢰하지 않는다면, 그가 판사
- 모두가 다른 누군가를 신뢰한다면, 판사가 없는 것
function findJudge(n: number, trust: number[][]): number {
const nobodyTrust = new Set<number>(Array.from({ length: n }, (_, i) => i + 1))
const trustCnt = Array.from({ length: n + 1 }, () => 0)
let maxTrustCnt = 0
for(const [from, to] of trust) {
maxTrustCnt = Math.max(maxTrustCnt, ++trustCnt[to])
nobodyTrust.delete(from)
}
if(maxTrustCnt !== n - 1) return -1
if(nobodyTrust.size) return Array.from(nobodyTrust)[0]
return -1
};