当前位置:网站首页>LeetCode Algorithm 997. Find the town judge

LeetCode Algorithm 997. Find the town judge

2022-06-12 01:42:00 Alex_ 12 hours a day 6 days a week

Topic link :997. Find the judge in town

Ideas

Algorithm : Count
data structure : chart
Ideas : Count the in and out degrees of each node , The degree of n-1, The degree of 0 The node of is the town judge .

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
原网站

版权声明
本文为[Alex_ 12 hours a day 6 days a week]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206120137090338.html