Created
April 6, 2022 09:43
-
-
Save Josephchinedu/4180e32db379e9784207516806fc1989 to your computer and use it in GitHub Desktop.
town judge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The rules: | |
#The town judge trusts nobody. | |
# Everybody (except for the town judge) trusts the town judge. | |
## let's look at the example: | |
# we've 3 people, and there trust is been displayed like this. [[1,2], [2,3]]. | |
# the trust simply means that 1 trust 2, and 2 trust 3. | |
# so 3 doesn't trust anyone that makes 3 the town judge | |
#### steps to solve it. | |
# 1. if len of trust is less than number of people - 1. we return false or -1 | |
# 2. how many number of people is trusting a number, if many people are trusting a number and the number is not trusting any one that makes the | |
# the number a town judge | |
## example on this steps: | |
## trust = [[[1,3], [1,4], [2,3], [2,4], [4,3]] | |
## and the number of people is 4 | |
## we need to create two arrays. number of trust coming in for a paticular person and number of trust going a particular person person is | |
# trusting how many people | |
## the rule is whenver a person is trusting someone, we increase their value by one in the "in array" | |
## also when a number is been trusted we increase the value by 1 | |
## let's check if out (trusted) is equal to number of people -1 and they're not trusting anyone | |
### example: | |
## trust = [[[1,3], [1,4], [2,3], [2,4], [4,3]] | |
# 0, 1, 2, 3, 4 | |
# in = [0, 2, 2, 0, 1] | |
# out = [0, 0, 0, 3, 2] | |
from typing import List | |
class Solution: | |
def findJudge(self, n: int, trust: List[List[int]]) -> int: | |
if len(trust) < n - 1: | |
return -1 | |
in_list = [0] * (n + 1) | |
out_list = [0] * (n + 1) | |
for trusting, trusted in trust: | |
in_list[trusted] += 1 | |
out_list[trusting] += 1 | |
for i in range(1, n + 1): | |
if in_list[i] == n - 1 and out_list[i] == 0: | |
return i | |
return -1 | |
g = Solution() | |
print(g.findJudge(3, [[1,3],[2,3]])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment