#2923

Find Champion I

Easy
ArrayMatrixArrayMatrix
LeetCode ↗

Approaches

Brute ForceOptimal
Complexity Comparison
Brute ForceOptimal Solution
Time
O(n²)
O(n)
Space
O(1)
O(1)
💡

Intuition

Time O(n)Space O(1)

We can find the champion in a single pass by leveraging the fact that if a team is stronger than another, it can only be the champion if no other team beats it. We can keep track of the potential champion and validate it in one go.

⚙️

Algorithm

5 steps
  1. 1Step 1: Initialize a variable `champion` to 0.
  2. 2Step 2: Iterate through each team `i` from 1 to n-1.
  3. 3Step 3: If grid[champion][i] == 1, it means the current champion is stronger than team i, so continue.
  4. 4Step 4: If grid[champion][i] == 0, it means team i is stronger than the current champion, so update `champion` to `i`.
  5. 5Step 5: After the loop, return the `champion`.
solution.py8 lines
1# Full working Python code
2
3def findChampion(grid):
4    champion = 0
5    for i in range(1, len(grid)):
6        if grid[champion][i] == 0:
7            champion = i
8    return champion

Complexity note: This complexity is optimal because we only make a single pass through the teams, resulting in linear time complexity.

  • 1The champion must be stronger than all other teams, which allows us to eliminate teams quickly.
  • 2The properties of the matrix (0s and 1s) help us determine relationships without needing to check every possible pair.

Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.