Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import defaultdict
- class Solution:
- def findJudge(self, N: int, trust: List[List[int]]) -> int:
- # If N is 1 and there are no relationships, return 1
- if N == 1 and len(trust) == 0:
- return 1
- # Create 2 dictionaries inbound and outbound
- inbound = defaultdict(int)
- outbound = defaultdict(int)
- # Iterate through trust array
- for relationship in trust:
- # Extract a_i and b_i
- a_i = relationship[0]
- b_i = relationship[1]
- # Increment a_i in outbound dict
- outbound[a_i] += 1
- # Increment b_i in inbound dict
- inbound[b_i] += 1
- # Iterate through inbound dict:
- for k, v in inbound.items():
- # If N-1 in inbound value for key k
- if v == N-1:
- # If the value for key k is 0 in outbound
- if outbound[k] == 0:
- # Return town judge if any.
- # Not possible to have 2 nodes that both satisfy town judge requirements.
- return k
- # If no town judge, return -1
- return -1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement