Find Champion I

Find Champion I

There are n teams numbered from 0 to n - 1 in a tournament.

Given a 0-indexed 2D boolean matrix grid of size n * n. For all i, j that 0 <= i, j <= n - 1 and i != j team i is stronger than team j if grid[i][j] == 1, otherwise, team j is stronger than team i.

Team a will be the champion of the tournament if there is no team b that is stronger than team a.

Return the team that will be the champion of the tournament.

LeetCode Problem - 2923

class Solution {
    public int findChampion(int[][] grid) {
        int n = grid.length; // Get the number of rows in the grid

        // Iterate through each row in the grid
        for (int i = 0; i < grid.length; i++) {
            int count = 0; // Initialize a counter to track the number of 1s in the current row

            // Iterate through each column in the current row
            for (int j = 0; j < grid[0].length; j++) {
                if (grid[i][j] == 1) { // Check if the current element is 1
                    count++; // Increment the counter if the element is 1
                }

                // If the count of 1s reaches the length of the row minus 1, it means this row is a "champion"
                if (count == grid[0].length - 1) {
                    return i; // Return the index of the champion row (0-based index)
                }
            }
        }

        return -1; // If no row is found that meets the criteria, return -1
    }
}

Did you find this article valuable?

Support Perf Insights by becoming a sponsor. Any amount is appreciated!