Re: [閒聊] 每日leetcode

作者: JerryChungYC (JerryChung)   2024-11-27 03:34:25
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
作者: Firstshadow (IamCatづミ'_'ミづ)   2024-11-27 03:39:00
大師, 另個解法是啥? 用set?
作者: cities516 (安安路過)   2024-11-27 03:59:00
直接找有沒有唯一一個不敗贏家就好
作者: sixB (6B)   2024-11-27 06:00:00
dag直接看in degree

Links booklink

Contact Us: admin [ a t ] ucptt.com