You are given an integer n, the number of teams in a tournament that has strange rules:
n / 2 matches are played, and n / 2 teams advance to the next round.(n - 1) / 2 matches are played, and (n - 1) / 2 + 1 teams advance to the next round.Return the number of matches played in the tournament until a winner is decided.
Example 1:
Input: n = 7 Output: 6 Explanation: Details of the tournament: - 1st Round: Teams = 7, Matches = 3, and 4 teams advance. - 2nd Round: Teams = 4, Matches = 2, and 2 teams advance. - 3rd Round: Teams = 2, Matches = 1, and 1 team is declared the winner. Total number of matches = 3 + 2 + 1 = 6.
Example 2:
Input: n = 14 Output: 13 Explanation: Details of the tournament: - 1st Round: Teams = 14, Matches = 7, and 7 teams advance. - 2nd Round: Teams = 7, Matches = 3, and 4 teams advance. - 3rd Round: Teams = 4, Matches = 2, and 2 teams advance. - 4th Round: Teams = 2, Matches = 1, and 1 team is declared the winner. Total number of matches = 7 + 3 + 2 + 1 = 13.
Constraints:
1 <= n <= 200Problem Overview: You are given n teams in a knockout tournament. Each round pairs teams to play matches, and the winner advances to the next round. If the number of teams is odd, one team automatically advances. The goal is to count the total number of matches played until a single champion remains.
Approach 1: Iterative Reduction Approach (Simulation) (Time: O(log n), Space: O(1))
This method directly simulates how the tournament progresses. In each round, compute how many matches are played based on the number of teams. If n is even, n / 2 matches occur and n / 2 teams advance. If n is odd, (n - 1) / 2 matches occur and one team automatically advances, leaving (n - 1) / 2 + 1 teams for the next round. Repeat this process using a loop until only one team remains, accumulating the total number of matches along the way. This is a straightforward simulation approach that mirrors the real tournament structure and is useful when practicing iterative reasoning problems.
Approach 2: Direct Mathematical Calculation (Time: O(1), Space: O(1))
The key insight comes from observing what happens in every match: exactly one team is eliminated. Starting with n teams and ending with one champion means n - 1 teams must be eliminated. Since each match eliminates exactly one team, the total number of matches played must be n - 1. This eliminates the need for simulation entirely. The result can be returned immediately with a simple formula. This solution relies on basic math reasoning rather than step‑by‑step iteration, making it the most efficient and elegant approach.
Recommended for interviews: Start with the simulation approach to show you understand how the tournament rounds work. Then present the mathematical insight that each match removes one team, leading to the formula n - 1. Interviewers typically expect the constant-time math solution because it demonstrates problem simplification and analytical thinking.
This approach involves treating the tournament rounds as iterative events where in each round, the number of matches is calculated, and non-advanced teams are eliminated until one team remains. This approach iterates over the number of teams until a winner is decided by subtracting the number of matches played from the total count of teams.
The function numberOfMatches uses a loop to repeatedly determine the number of matches in each round and adjust the number of teams until a single team remains. If the number of teams n is even, n / 2 matches are played. If n is odd, (n - 1) / 2 matches occur, and one team directly advances.
Time Complexity: O(log n) since in each step the number of teams is halved.
Space Complexity: O(1) since we only use a constant amount of additional memory.
Instead of simulating each round, this approach uses the observation that to find a winner among n teams, exactly n - 1 matches must be played. This is because each match eliminates exactly one team and n - 1 eliminations leave one champion.
The function numberOfMatches directly returns n - 1 under the logical deduction that each match will remove one team until only one team remains.
Time Complexity: O(1)
Space Complexity: O(1)
From the problem description, we know that there are n teams in total. Each pairing will eliminate one team. Therefore, the number of pairings is equal to the number of teams eliminated, which is n - 1.
The time complexity is O(1), and the space complexity is O(1).
Python
Java
C++
Go
TypeScript
JavaScript
| Approach | Complexity |
|---|---|
| Iterative Reduction Approach | Time Complexity: O(log n) since in each step the number of teams is halved. |
| Direct Mathematical Calculation | Time Complexity: O(1) |
| Quick Thinking | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Iterative Reduction (Simulation) | O(log n) | O(1) | When you want to simulate tournament rounds or demonstrate step-by-step reasoning |
| Direct Mathematical Calculation | O(1) | O(1) | Best choice when recognizing that each match eliminates exactly one team |
Count of Matches in Tournament | MICROSOFT | Leetcode 1688 • codestorywithMIK • 6,624 views views
Watch 9 more video solutions →Practice Count of Matches in Tournament with our built-in code editor and test cases.
Practice on FleetCode