-
Notifications
You must be signed in to change notification settings - Fork 863
Expand file tree
/
Copy pathProblem1.py
More file actions
28 lines (22 loc) · 861 Bytes
/
Copy pathProblem1.py
File metadata and controls
28 lines (22 loc) · 861 Bytes
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
# https://leetcode.com/problems/find-the-town-judge/description/
# Graph problem
# Did this code successfully run on Leetcode : Yes
# Any problem you faced while coding this : No
# TC: O(E) + O(V) => We need to first go over the entire input array of tuples (edges) to construct the indegrees
# then we need to traverse the entire indegrees array of size V
# SC: O(V) => indegrees array of size V
class Solution:
def findJudge(self, n: int, trust: List[List[int]]) -> int:
indegrees = [0] * (n+1)
if n == 1:
return 1
for i in range(len(trust)):
tr = trust[i]
# giving trust
indegrees[tr[0]] -= 1
# receiving trust
indegrees[tr[1]] += 1
for i in range(len(indegrees)):
if indegrees[i] == n-1:
return i
return -1