problems(swift): add “1688. Count of Matches in Tournament”
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
fba591dc92
commit
75305c4b8d
1 changed files with 19 additions and 0 deletions
19
problems/swift/count-of-matches-in-tournament.swift
Normal file
19
problems/swift/count-of-matches-in-tournament.swift
Normal file
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue