2924. Find Champion II
有看到更好的解法 不過還是寫自己第一次過的好了
預設每隊是-1 不過其實預設為1就好 輸才要改掉
class Solution:
def findChampion(self, n: int, edges: List[List[int]]) -> int:
if not edges:
return 0 if n == 1 else -1
teams = {team: -1 for team in range(n)}
for win, los in edges:
if teams[win] == -1:
teams[win] = 1
if teams[los] != 0:
teams[los] = 0
winner = -1
for t in teams:
if teams[t] == 1:
if winner == -1:
winner = t
else:
return -1
elif teams[t] == -1:
return -1
return winner