티스토리 뷰

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
    }
}
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
글 보관함