-
Notifications
You must be signed in to change notification settings - Fork 0
/
Find the Town Judge - 10th May
16 lines (15 loc) · 963 Bytes
/
Find the Town Judge - 10th May
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Question - In a town, there are N people labelled from 1 to N. There is a rumor that one of these people is secretly the town judge.
If the town judge exists, then:
1. The town judge trusts nobody.
2. Everybody (except for the town judge) trusts the town judge.
3. There is exactly one person that satisfies properties 1 and 2.
You are given trust, an array of pairs trust[i] = [a, b] representing that the person labelled a trusts the person labelled b.
If the town judge exists and can be identified, return the label of the town judge. Otherwise, return -1.
Solution 1 -
def findJudge(self, N: int, trust: List[List[int]]) :
trusted = dict.fromkeys(range(1,N+1), 0)
for a,b in trust:
trusted[a] -= 1
trusted[b] += 1
key = max(trusted, key=trusted.get) # it will return the key for max value and max value will be possible only for jUDGE IF ITS EQUAL TO n-1
return key if trusted[key]==N-1 else -1