DSA

Tournament Winner

Arrays. Time O(n), Space O(n).

August 8, 2026

Approach#

Walk through every match in order. For each match, determine the winner from the results array (1 = home team wins, 0 = away team wins), then increment that team's score in a hash map. By also maintaining a running maxScore and winningTeam, we avoid a second scan over the map at the end — the tournament winner is always the team that last pushed maxScore higher. This runs in O(n) time and O(n) space where n is the number of matches.

  • Use hashmap to store the scores of the teams.
  • Keep note of maxScore and winningTeam at each iteration.
cpp
#include <vector>
using namespace std;

string tournamentWinner(
  vector<vector<string>> competitions, vector<int> results
) {
  unordered_map<string, int> scoreMap;

  int totalMatches = competitions.size();
  int maxScore = 0;
  string winningTeam = "";

  for(int match = 0; match < totalMatches; match++){
    
    string homeTeam = competitions[match][0];
    string awayTeam = competitions[match][1];

    string winner = results[match] == 1 ? homeTeam : awayTeam;

    scoreMap[winner]++;

    if(scoreMap[winner] > maxScore){
        maxScore = scoreMap[winner];
        winningTeam = winner;
      }   
  }
  return winningTeam;
}

Time Complexity: O(n)

Space Complexity: O(n)