Solution to LeetCode 1688 Count of Matches in Tournament.
LeetCode 1688
Count of Matches in Tournament (Easy). [link]
1] Mimic the process in the problem statement.
class Solution(object):
def numberOfMatches(self, n):
"""
:type n: int
:rtype: int
"""
ans = 0
while n > 1:
if n % 2 == 0: # even
ans += n // 2
n //= 2
else:
ans += (n-1) // 2
n = (n-1) // 2 + 1
return ans
2] Math trick. n-1 teams fail, so there are n-1 rounds of tournament.
class Solution(object):
def numberOfMatches(self, n):
"""
:type n: int
:rtype: int
"""
return n-1