0
点赞
收藏
分享

微信扫一扫

LeetCode Algorithm 997. 找到小镇的法官

Ideas

算法:计数
数据结构:图
思路:统计每个节点的入度和出度,入度为n-1,出度为0的节点即为小镇法官。

Code

Python

from collections import Counter
from typing import List


class Solution:
def findJudge(self, n: int, trust: List[List[int]]) -> int:
ans, in_degree, out_degree = -1, Counter(), Counter()
for (start, end) in trust:
in_degree[end] += 1
out_degree[start] += 1
for i in range(1, n + 1):
if in_degree[i] == n - 1 and out_degree[i] == 0:
ans = i
return ans


举报

相关推荐

0 条评论