Count of Matches in Tournament
Easy · Math & Geometry · Explanation and code example
Math
How it works
Every match eliminates exactly one team. To go from n teams to 1 winner, we need to eliminate n - 1 teams. Therefore, exactly n - 1 matches are played regardless of the tournament bracket structure.
Steps
- Return
n - 1.
Complexity
- Time complexity:
O(1) - Space complexity:
O(1)
Things to watch for
Overcomplicating with Simulation
Many beginners implement a full round-by-round simulation when the mathematical insight n - 1 gives the answer directly. Each match eliminates exactly one team, and to get from n teams to 1 winner requires eliminating n - 1 teams.
Off-by-One in Winner Calculation
When simulating, incorrectly calculating the number of teams advancing to the next round. The correct formula is (n + 1) / 2 (integer division), not n / 2, because the bye team also advances.
Python code example
class Solution:
def numberOfMatches(self, n: int) -> int:
return n - 1
C++ code example
class Solution {
public:
int numberOfMatches(int n) {
return n - 1;
}
};
Source and license
Adapted from NeetCode’s source · MIT license. Node types are supplied by LeetCode where needed.