[LeetCode] 1688. Count of Matches in Tournament

문제 보기

Kotlin

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
fun numberOfMatches(n: Int): Int {
var remains = n
var cnt = 0

while (remains > 1) {
if (remains % 2 == 0) {
cnt += remains / 2
remains /= 2
} else {
cnt += (remains - 1) / 2
remains = (remains - 1) / 2 + 1
}
}

return cnt
}
}

댓글