Count of Matches in Tournament (LeetCode 1688): Solution and Explanation

Easy
Math
Simulation

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

  1. Start with matches = 0.
  2. While more than one team is left, compute played = n // 2 and add it to matches.
  3. Advance the winners and the bye team: n = played + n % 2.
  4. When one team is left, return matches.

Step-by-step example

n = 7
  1. 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.
  2. Round 2: 4 teams, 2 matches. matches = 5, and 2 teams advance.
  3. Round 3: 2 teams, 1 match. matches = 6, and 1 team is left.
  4. 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 matches
Press play on the visualizer and the running line lights up here.

Time and space complexity

Work as the input growsinput size n →steps
Return n - 1 O(1)
Simulate the rounds O(log n)
Two branches per round O(log n)
Time

O(log n)

The number of teams roughly halves each round. The n - 1 formula is O(1).

Space

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.