1688. Count of Matches in Tournament - Explanation

Problem Link



1. Simulation

class Solution:
    def numberOfMatches(self, n: int) -> int:
        res = 0

        while n > 1:
            res += n // 2
            n = (n + 1) // 2

        return res

Time & Space Complexity

  • Time complexity: O(logn)O(\log n)
  • Space complexity: O(1)O(1)

2. Math

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

Time & Space Complexity

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