Intuition¶
The problem revolves around identifying a "champion" in a Directed Acyclic Graph (DAG) based on specific rules.
Approach¶
- Tracking Defeated Teams:
- Use a bitset of size
100to track teams that have been defeated (i.e., have incoming edges). - For every directed edge
[u, v], mark v in the losses bitset, as it indicates thatvhas been defeated byu. - Identifying the Champion:
- Iterate through all n teams.
- If a team i has not been defeated
(losses[i] == false), it is a potential champion. - If more than one such team is found,
return -1immediately, as there is no unique champion. - Return the Result:
- If exactly one team has no incoming edges, its index is returned as the champion.
- If all teams have been defeated, champion remains -1, and the function
returns -1.
Complexity¶
- Time complexity: O(m+n)
- Marking defeated teams O(m), where
mis the number of edges (size of edges). -
Checking all teams O(n), where
nis the number of teams. -
Space complexity: O(n)