-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path2924-find-champion-ii.py
More file actions
43 lines (37 loc) · 1 KB
/
2924-find-champion-ii.py
File metadata and controls
43 lines (37 loc) · 1 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# time complexity: O(n)
# space complexity: O(n)
from collections import defaultdict
from typing import List
class Solution:
def findChampion(self, n: int, edges: List[List[int]]) -> int:
indegree = defaultdict(int)
for i in range(n):
indegree[i] = 0
for edge in edges:
indegree[edge[1]] += 1
champion = -1
championCount = 0
for key, count in indegree.items():
if count == 0:
champion = key
championCount += 1
print(indegree)
return champion if championCount == 1 else -1
n = 3
edges = [[0, 1], [1, 2]]
print(Solution().findChampion(n, edges))
n = 4
edges = [[0, 2], [1, 3], [1, 2]]
print(Solution().findChampion(n, edges))
n = 3
edges = [[0, 1], [2, 1]]
print(Solution().findChampion(n, edges))
n = 1
edges = [[2, 1]]
print(Solution().findChampion(n, edges))
n = 2
edges = []
print(Solution().findChampion(n, edges))
n = 3
edges = [[0, 1]]
print(Solution().findChampion(n, edges))