LeetCode 1688、比赛中的配对次数
LeetCode 1688、比赛中的配对次数
# 1688. 比赛中的配对次数
class Solution:
def numberOfMatches(self, n: int) -> int:
# 结果变量
ans = 0
# 不断的配对比赛,直到剩下一支队伍为止
while n > 1:
# 1、偶数支队伍
if n % 2 == 0:
# 总共进行 n / 2 场比赛
ans += n // 2
# 同时剩下了 n / 2 支队伍
n //= 2
# 2、奇数支队伍
else:
# 总共进行 ( n - 1 ) / 2 场比赛
ans += (n - 1) // 2
# 同时剩下了 (n - 1) / 2 + 1 支队伍
n = (n - 1) // 2 + 1
# 获取结果
return ans