From 75305c4b8d94448c7c9573c95f5acd75cfa0c75b Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Sat, 9 Dec 2023 13:01:47 +0100 Subject: [PATCH] =?UTF-8?q?problems(swift):=20add=20=E2=80=9C1688.=20Count?= =?UTF-8?q?=20of=20Matches=20in=20Tournament=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- .../count-of-matches-in-tournament.swift | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 problems/swift/count-of-matches-in-tournament.swift diff --git a/problems/swift/count-of-matches-in-tournament.swift b/problems/swift/count-of-matches-in-tournament.swift new file mode 100644 index 0000000..63e0359 --- /dev/null +++ b/problems/swift/count-of-matches-in-tournament.swift @@ -0,0 +1,19 @@ +class Solution { + func numberOfMatches(_ n: Int) -> Int { + var n = n + + var matches = 0 + while n > 1 { + if n % 2 == 0 { + matches += n >> 1; + } else { + matches += n >> 1; + n += 2; + } + + n >>= 1; + } + + return matches + } +}