|
| 1 | +# Tournament Winner |
| 2 | + |
| 3 | +There's an algorithms tournament taking place in which teams of programmers |
| 4 | +compete against each other to solve algorithmic problems as fast as possible. |
| 5 | +Teams compete in a round robin, where each team faces off against all other |
| 6 | +teams. Only two teams compete against each other at a time, and for each |
| 7 | +competition, one team is designated the home team, while the other team is the |
| 8 | +away team. In each competition there's always one winner and one loser; there |
| 9 | +are no ties. A team receives 3 points if it wins and 0 points if it loses. The |
| 10 | +winner of the tournament is the team that receives the most amount of points. |
| 11 | + |
| 12 | +Given an array of pairs representing the teams that have competed against each |
| 13 | +other and an array containing the results of each competition, write a |
| 14 | +function that returns the winner of the tournament. The input arrays are named |
| 15 | +`competitions` and `results`, respectively. The |
| 16 | +`competitions` array has elements in the form of |
| 17 | +`[homeTeam, awayTeam]`, where each team is a string of at most 30 |
| 18 | +characters representing the name of the team. The `results` array |
| 19 | +contains information about the winner of each corresponding competition in the |
| 20 | +`competitions` array. Specifically, `results[i]` denotes |
| 21 | +the winner of `competitions[i]`, where a <span>1</span> in the |
| 22 | +`results` array means that the home team in the corresponding |
| 23 | +competition won and a `0` means that the away team won. |
| 24 | + |
| 25 | +It's guaranteed that exactly one team will win the tournament and that each |
| 26 | +team will compete against all other teams exactly once. It's also guaranteed |
| 27 | +that the tournament will always have at least two teams. |
| 28 | + |
| 29 | +```plain |
| 30 | +Sample Input |
| 31 | +
|
| 32 | +competitions = [["HTML", "C#"], ["C#", "Python"], ["Python", "HTML"]] |
| 33 | +results = [0, 0, 1] |
| 34 | +
|
| 35 | +Sample Output |
| 36 | +"Python" |
| 37 | +
|
| 38 | +Explanation |
| 39 | +C# beats HTML, Python beats C#, and Python beats HTML. |
| 40 | +HTLM - 0 points |
| 41 | +C# - 3 points |
| 42 | +Python - 6 points |
| 43 | +That means that Python is the tournament winner and we return "Python". |
| 44 | +``` |
| 45 | + |
| 46 | +## Hints |
| 47 | + |
| 48 | +1. Don't overcomplicate this problem. How would you solve it by hand? Consider that approach and try to translate it |
| 49 | + into code |
| 50 | +2. Use a hash table to store the total points collected by each team, with the team names as the keys in the hash table. |
| 51 | + Once you know how many points each team has, how can you determine which one is the winner? |
| 52 | +3. Loop through all of the competitions, and update the hash table at every |
| 53 | + iteration. For each competition, consider the name of the winning team; if the |
| 54 | + name already exists in the hash table, update that entry by adding 3 points to |
| 55 | + it. If the team name doesn't exist in the hash table, add a new entry in the |
| 56 | + hash table with the key as the team name and the value as 3 (since the team |
| 57 | + won its first competition). While looping through all of the competitions, |
| 58 | + keep track of the team with the highest score, and at the end of the |
| 59 | + algorithm, return the team with the highest score. |
| 60 | + |
| 61 | +## Optimal Space & Time Complexity |
| 62 | + |
| 63 | +O(n) time | O(k) space - where n is the number of competitions and k is the number of teams |
0 commit comments