Leetcode 2923: Find Champion I

grid47
grid47
Exploring patterns and algorithms
Jan 19, 2024 4 min read

In a tournament, there are ’n’ teams, each represented by an index from 0 to n-1. You are given a 2D boolean matrix grid of size n x n, where the value at grid[i][j] is 1 if team ‘i’ is stronger than team ‘j’, and 0 otherwise. Your task is to determine the champion team, which is the team that no other team is stronger than.
Problem
Approach
Steps
Complexity
Input: The input consists of a 2D boolean matrix grid where grid[i][j] indicates if team 'i' is stronger than team 'j'.
Example: For input grid = [[0, 1], [0, 0]], the result is 0.
Constraints:
• 2 <= n <= 100
• grid[i][j] is either 0 or 1
• grid[i][i] == 0
• For all i != j, grid[i][j] != grid[j][i]
Output: Return the index of the team that is the champion, or -1 if no such team exists.
Example: For input grid = [[0, 1], [0, 0]], the output is 0.
Constraints:
• The output will always be a valid team index or -1.
Goal: The goal is to find the team that no other team can beat, i.e., the team that has a '1' in every column of the matrix corresponding to the teams that it is stronger than.
Steps:
• Iterate through each row of the matrix.
• Sum the values in the row (excluding the diagonal).
• If the sum of a row equals n-1, that team is the champion.
Goal: The size of the grid will always match the number of teams, and it will adhere to the following constraints.
Steps:
• The number of teams, n, will always be between 2 and 100.
• Each row will contain exactly one 1 for every team the team is stronger than.
Assumptions:
• The grid matrix will always be valid and well-formed.
Input: Example 1: grid = [[0, 1], [0, 0]]
Explanation: Here, team 0 is stronger than team 1, and there are only two teams. Thus, team 0 will be the champion.

Input: Example 2: grid = [[0, 0, 1], [1, 0, 1], [0, 0, 0]]
Explanation: In this case, team 1 is stronger than both team 0 and team 2, making team 1 the champion.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus