Problem Description
Given a tournament of n teams (numbered 0 to n - 1) and a 0-indexed 2D boolean matrix grid where for every pair of teams (i, j) with i ≠ j, grid[i][j] == 1 indicates that team i is stronger than team j, determine the champion. A team is declared the champion if there is no other team that is stronger than it.
Key Insights
- Each team is compared against every other team based on the grid values.
- A team qualifies as champion if no other team has a winning result over it.
- The problem guarantees that if team a is stronger than team b and team b is stronger than team c, then team a is stronger than team c (transitivity).
- A brute force check for each team across all other teams solves the problem efficiently within the given constraints.
Space and Time Complexity
Time Complexity: O(n^2) due to the nested loops checking each team against all others. Space Complexity: O(1) as the solution does not require extra space proportional to input size.
Solution
The solution involves iterating through each team and treating it as a potential champion. For each team, we check if there is any opponent such that grid[opponent][candidate] == 1, which would mean that the opponent is stronger than the candidate. If no such opponent is found, the candidate is the champion. The transitivity of the relationships guarantees that the identified champion is indeed the overall champion.