티스토리 뷰
https://leetcode.com/problems/count-of-matches-in-tournament/submissions/
Count of Matches in Tournament - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
토너먼트 경기가 진행된다.
참가하는 팀의 숫자가 주어진다.
토너먼트경기로 치뤄지며, 팀 숫자가 홀수팀일 경우 부전승 팀이 생긴다.
총 진행되는 경기 수를 반환
impl Solution {
pub fn number_of_matches(mut n: i32) -> i32 {
if n == 1 { return 0; }
let mut result = 0;
let mut matches = 0;
while n != 1 {
if n % 2 == 0 {
n = n / 2;
matches = n;
}else{
n -= 1;
n = n / 2;
matches = n;
n += 1;
}
result += matches;
}
result
}
}
'알고리즘 문제 풀이' 카테고리의 다른 글
[알고리즘문제] 릿코드 Pascal's Triangle (0) | 2021.07.10 |
---|---|
[알고리즘문제] 릿코드 Employee Importance (0) | 2021.06.11 |
[알고리즘문제] 릿코드 Count and Say (0) | 2021.05.29 |
[알고리즘문제] 릿코드 Rotate Image (0) | 2021.05.26 |
[알고리즘문제] 릿코드 Climbing Stairs (0) | 2021.05.19 |