반응형

https://leetcode.com/problems/count-of-matches-in-tournament/description

 

Count of Matches in Tournament - LeetCode

Can you solve this real interview question? Count of Matches in Tournament - You are given an integer n, the number of teams in a tournament that has strange rules: * If the current number of teams is even, each team gets paired with another team. A total

leetcode.com

 

반복문을 이용해 로그 시간에 풀 수 있었습니다.

class Solution {
public:
    int numberOfMatches(int n) {
        int res = 0;
        while(n > 1) {
            res += n / 2;
            n = n / 2 + n % 2;
        }
        return res;
    }
};

 

상수 시간에 풀 수 있었습니다.

class Solution {
public:
    int numberOfMatches(int n) {
        return n - 1;
    }
};
반응형

+ Recent posts