-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlc-997.py
38 lines (31 loc) · 959 Bytes
/
lc-997.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
'''
LC-997 - Find the Town Judge
Algo: Graph / HashMap
Type: Graph
Language: Python3
Author: Rishabh IO
Notes: TC can be further improved, this is not
the most optimal solution.
'''
from collections import defaultdict
class Solution:
def findJudge(self, n: int, trust):
potential_judges = [ True ] * (n + 1)
trustees = defaultdict(set)
for t in trust:
eliminate = t[0]
trustees[t[1]].add( eliminate )
potential_judges[eliminate] = False
judge = -1
for i in range( 1, n+1):
if potential_judges[i] != False:
judge = i
if judge == -1:
return judge
# validate whether everybody trusts the judge:
for i in range( 1, n ):
if i == judge:
continue
if i not in trustees[judge]:
return -1
return judge