Count of matches in tournament

November 15, 2022

math-and-geometry

Problem URL: Count of matches in tournament

We can simply count the number of matches played by counting the number of times we divide the number of teams by 2. We can also count the number of teams by counting the number of times we multiply the number of teams by 2.

class Solution:
    def numberOfMatches(self, n: int) -> int:
        matches = 0
        while n > 1:
            matches += n // 2
            n = n // 2 + n % 2
        return matches

Time complexity: O(log(n))
Space complexity: O(1)

We can take another approach. For every match, one win and other loses and get eliminated. The champion never lose, n-1 other other team lose. So, the total number of matches played is n-1.

class Solution:
    def numberOfMatches(self, n: int) -> int:
        return n-1

Time complexity: O(1)
Space complexity: O(1)