Count of Matches in Tournament (LeetCode 1688): Solution and Explanation
A knockout tournament starts with n teams. Each round pairs teams into matches, and when the count is odd one team gets a bye. Return the total number of matches until one team remains.
Intuition
The rules describe a simulation directly. With n teams in a round, n // 2 matches are played. The winners advance, and if n is odd the unpaired team advances too, so the next round has n // 2 + n % 2 teams.
The count roughly halves every round, so the loop runs about log2(n) times and the total is just the sum of the matches per round.
There is a shortcut hiding in the tally. Every match sends exactly one team home, and every team except the champion goes home exactly once. So the number of matches equals the number of losers, which is n - 1, no matter how the byes fall.
Approach: Simulate the rounds
- Start with
matches = 0. - While more than one team is left, compute
played = n // 2and add it tomatches. - Advance the winners and the bye team:
n = played + n % 2. - When one team is left, return
matches.
Step-by-step example
n = 7
- Round 1: 7 teams. 7 is odd, so 3 matches are played and 1 team gets a bye. matches = 3, and 3 + 1 = 4 teams advance.
- Round 2: 4 teams, 2 matches. matches = 5, and 2 teams advance.
- Round 3: 2 teams, 1 match. matches = 6, and 1 team is left.
- One team remains, so return 6, which is 7 - 1.
Code: Python, Java, C++
1class Solution:
2 def numberOfMatches(self, n: int) -> int:
3 matches = 0
4 while n > 1:
5 played = n // 2
6 matches += played
7 n = played + n % 2
8 return matches1class Solution {
2 public int numberOfMatches(int n) {
3 int matches = 0;
4 while (n > 1) {
5 int played = n / 2;
6 matches += played;
7 n = played + n % 2;
8 }
9 return matches;
10 }
11}1class Solution {
2public:
3 int numberOfMatches(int n) {
4 int matches = 0;
5 while (n > 1) {
6 int played = n / 2;
7 matches += played;
8 n = played + n % 2;
9 }
10 return matches;
11 }
12};Time and space complexity
O(log n)
The number of teams roughly halves each round. The n - 1 formula is O(1).
O(1)
Only the counters n, played and matches are stored.
Other approaches
Return n - 1 (time O(1), space O(1))
Each match eliminates one team and every team but the winner is eliminated exactly once, so the answer is n - 1 with no loop at all.
Two branches per round (time O(log n), space O(1))
Follow the statement literally with an if for even n (n / 2 matches, n / 2 advance) and an else for odd n ((n - 1) / 2 matches, (n - 1) / 2 + 1 advance). Same result, a little more code.
Common mistakes
- Dropping the bye team when n is odd. The next round has n // 2 + 1 teams, not n // 2.
- Looping while n > 0 instead of n > 1, which plays a phantom round with a single team.
- Forgetting that n = 1 means no matches at all, so the answer is 0.
Frequently asked questions
Why is the answer always n - 1?
Each match produces exactly one loser, and a team that loses is out for good. At the end every team except the champion has lost once, so there were n - 1 losses and therefore n - 1 matches.
Does the bye change the total?
No. A bye only delays when a team plays. The number of eliminations needed to leave one team is still n - 1.
Which solution should I give in an interview?
Show the simulation to prove you read the rules, then point out the one-loser-per-match argument and reduce it to n - 1.