## Tournament Winner

There’s an algorithms tournament taking place in which teams of programmers compete against each other to solve algorithmic problems as fast as possible. Teams compete in a round robin, where each team faces off against all other teams. Only two teams compete against each other at a time, and for each competition, one team is designated the home team, while the other team is the away team. In each competition there’s always one winner and one loser; there are no ties. A team receives 3 points if it wins and 0 points if it loses. The winner of the tournament is the team that receives the most amount of points.

Given an array of pairs representing the teams that have competed against each other and an array containing the results of each competition, write a function that returns the winner of the tournament. The input arrays are named competitions and results, respectively. The competitions array has elements in the form of [homeTeam, awayTeam], where each team is a string of at most 30 characters representing the name of the team. The results array contains information about the winner of each corresponding competition in the competitions array. Specifically, results[i] denotes the winner of competitions[i], where a 1 in the results array means that the home team in the corresponding competition won and a 0 means that the away team won.

It’s guaranteed that exactly one team will win the tournament and that each team will compete against all other teams exactly once. It’s also guaranteed that the tournament will always have at least two teams.

### Sample Input

`1competitions = [2 ["HTML", "C#"],3 ["C#", "Python"],4 ["Python", "HTML"],5]6results = [0, 0, 1]`

### Sample Output

`1"Python"2// C# beats HTML, Python Beats C#, and Python Beats HTML.3// HTML - 0 points4// C# - 3 points5// Python - 6 points`

### Hints

**Hint 1**

Don’t overcomplicate this problem. How would you solve it by hand? Consider that approach, and try to translate it into code.

**Hint 2**

Use a hash table to store the total points collected by each team, with the team names as keys in the hash table. Once you know how many points each team has, how can you determine which one is the winner?

**Hint 3**

Loop through all of the competitions, and update the hash table at every iteration. For each competition, consider the name of the winning team; if the name already exists in the hash table, update that entry by adding 3 points to it. If the team name doesn’t exist in the hash table, add a new entry in the hash table with the key as the team name and the value as 3 (since the team won its first competition). While looping through all of the competitions, keep track of the team with the highest score, and at the end of the algorithm, return the team with the highest score.

**Optimal Space & Time Complexity**

O(n) time | O(k) space - where n is the number of competitions and k is the number of teams

```
1/* competitions = [2 ["html", "C#"]3 ["c#", "Python"]4 ["python", "html"]5 ]6 results = [0, 0, 1]7 8 // loop over competitions9 // make a dict with the name of the team10 // increament the team number which wins11 // return the team with the largest number of wins12*/13
14function tournamentWinner(competitions, results) {15 const teams = {}16 for (let i = 0; i < competitions.length; i++) {17 if (results[i] === 1) {18 teams[competitions[i][0]] = teams[competitions[i][0]] + 3 || 319 } else {20 teams[competitions[i][1]] = teams[competitions[i][1]] + 3 || 321 }22 }23 if (Object.keys(teams).length > 0) {24 let maxValue = Object.keys(teams).reduce((a, b) => teams[a] > teams[b] ? a : b)25 return maxValue26 }27 return [];28}
```

```
1const HOME_TEAM_WON = 12
3function tournamentWinner(competitions, results) {4 let currentBestTeam = '';5 const scores = {[currentBestTeam]: 0}6 7 for (let i = 0; i < competitions.length; i++) {8 const result = results[i];9 let [homeTeam, awayTeam] = competitions[i];10 11 const winningTeam = result === HOME_TEAM_WON ? homeTeam: awayTeam;12 13 updateScore(winningTeam, 3, scores)14 15 if (scores[winningTeam] > scores[currentBestTeam]) {16 currentBestTeam = winningTeam; 17 }18 }19 return currentBestTeam;20}21
22function updateScore(team, points, scores) {23 if (!(team in scores)) scores[team] = 0;24 scores[team] += points;25}26
27// O(n) time, where n is the number of matches28// O(k) space, where k is the number of teams
```

🌂