Leetcode Note: Go - Find the Town Judge

Find the Town Judge - LeetCode
https://leetcode.com/problems/find-the-town-judge/

  • Go 言語で取り組んだメモ

回答

Go Solution Graph - LeetCode Discuss
https://leetcode.com/problems/find-the-town-judge/discuss/859111/Go-Solution-Graph

func findJudge(N int, trust [][]int) int {
	if len(trust) == 0 && N == 1 {
		return 1
	}

	// out, in
	edges := make([][2]int, N+1)
	candidates := []int{}
	for _, t := range trust {
		edges[t[0]][0]++
		edges[t[1]][1]++
		if edges[t[1]][1] == N-1 {
			candidates = append(candidates, t[1])
		}
	}

	for _, c := range candidates {
		if edges[c][0] == 0 {
			return c
		}
	}
	return -1
}