diff options
author | jack <[email protected]> | 2024-06-14 13:13:15 -0600 |
---|---|---|
committer | jack <[email protected]> | 2024-06-14 13:13:15 -0600 |
commit | d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch) | |
tree | 96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /count_matches_tournament.py | |
download | leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip |
initial
Diffstat (limited to 'count_matches_tournament.py')
-rw-r--r-- | count_matches_tournament.py | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/count_matches_tournament.py b/count_matches_tournament.py new file mode 100644 index 0000000..124f5c3 --- /dev/null +++ b/count_matches_tournament.py @@ -0,0 +1,32 @@ +# 1688. Count of Matches in Tournament + +""" +given an integer 'n', the number of teams in a tournament that has strange +rules: if the current number of teams is even, each team get paired with +another team. a total of 'n / 2' matches are played, and 'n / 2' teams +advance to the next round. if the current number of teams is odd, one team +randomly advances in the tournament and the rest gets paired. a total of '(n +- 1) / 2' matches are played, and '(n - 1) / 2 + 1' teams advance to the next +round. return the number of matches played in the tournament until a winner +is decided +""" + + +class Solution(object): + def numberOfMatches(self, n): + """ + :type n: int + :rtype: int + """ + ans = 0 + while n > 1: + ans += n // 2 + n = (n + 1) // 2 + return ans + + +if __name__ == "__main__": + obj = Solution() + print(obj.numberOfMatches(7)) # expect: 6 + print(obj.numberOfMatches(14)) # expect: 13 + print(obj.numberOfMatches(672)) # expect: 671 |