Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def findJudge(self, n: int, trust: List[List[int]]) -> int:
- out = Counter(ele[0] for ele in trust)
- inc = Counter(ele[1] for ele in trust)
- for i in range(1, n+1):
- if out[i] == 0 and inc[i] == n-1:
- return i
- return -1
- #using one hashtable
- '''
- """
- Why mp[i] == n-1 works?
- Our goal is to find a node which has 0 outgoing edges and n-1 incoming edges.(at most there can be only one such node)
- So in mp[i] we are storing the count of incoming edges in excess to the outgoing edges.
- So mp[i] == n-1 means that for node i there are n-1 more incoming edges than outgoing ones, but we know that at any particular node there cannot be more than n-1 total edges (incoming + outgoing), so in this case outgoing edges has to be zero and incoming edges n-1, which is the judge condition.
- By the same reasoning all non-judge nodes will have values less than n-1(either due to shortage of incoming edges or presence of outgoing edge)
- """
- class Solution:
- def findJudge(self, n: int, trust: List[List[int]]) -> int:
- mp = Counter()
- for a, b in trust:
- mp[b] += 1
- mp[a] -= 1
- for i in range(1, n+1):
- if mp[i] == n-1:
- return i
- return -1
- '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement