Tournament Winner

Tournament Winner

Category

Arrays

Difficulty

Easy

Problem Statement

Given a list of competition pairs and a corresponding results array, determine the winner of a round-robin tournament. Each competition has a home team and an away team. The results array contains 0 (away team wins) or 1 (home team wins) for each match. The team with the most points wins. Each victory awards 3 points. There are no ties.

Intuition

Since every match produces a clear winner, we simply tally up points for each team using a hash map. The team with the highest total at the end is the tournament winner. One pass through the competitions is sufficient.

Approach

  1. Initialize a hash map to track each team's score.
  2. Initialize a variable to track the current best team and its score.
  3. Iterate through each competition alongside its result.
  4. Determine the winner of the current match from the result.
  5. Add 3 points to that team's score in the hash map.
  6. If that team's score now exceeds the current best, update the best team.
  7. Return the best team after all matches are processed.

Pseudocode

function tournamentWinner(competitions, results):
    scores = empty hash map
    bestTeam = ""
    bestScore = 0
    for i from 0 to length(competitions) - 1:
        [homeTeam, awayTeam] = competitions[i]
        winner = homeTeam if results[i] == 1 else awayTeam
        scores[winner] = scores.getOrDefault(winner, 0) + 3
        if scores[winner] > bestScore:
            bestScore = scores[winner]
            bestTeam = winner
    return bestTeam

Time & Space Complexity

  • Time: O(n) — where n is the number of competitions. Each match is processed once.
  • Space: O(k) — where k is the number of distinct teams, for the hash map.

Key Insights

  • Tracking the running best avoids a second pass to find the maximum at the end.
  • The problem guarantees no ties and exactly one winner, simplifying the logic.
  • Each match awards exactly 3 points to the winner (0 to the loser), which is the standard tournament scoring.

Edge Cases

  • Only one competition — the winner of that match is the tournament winner.
  • A team wins all its matches — it will be detected as best early and remain best.
  • All teams have only one match — the result is simply the winner of the single match involving the most wins (though the problem guarantees a unique winner).