넘치게 채우기

[LeetCode] 1688. Count of Matches in Tournament 본문

PS/LeetCode

[LeetCode] 1688. Count of Matches in Tournament

riveroverflow 2023. 12. 5. 09:55
728x90
반응형

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

leetcode - Count of Matches in Tournament

문제 유형 : 수학

문제 난이도 : Easy

 

문제

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 of n / 2 matches are played, and n / 2 teams advance to the next round.
  • If the current number of teams is odd, one team randomly advances in the tournament, and the rest gets paired. A total of (n - 1) / 2 matches are played, and (n - 1) / 2 + 1 teams advance to the next round.

Return the number of matches played in the tournament until a winner is decided.

 

당신은 정수 n(팀의 수)을 받는다. 토너먼트는 다음의 규칙을 따른다.

만약 현재 팀의 수가 짝수이면 n/2개의 팀이 다음 라운드에 진출한다.

만약 홀수이면 n/2+1개의 팀이 다음 라운드에 진출한다.

토너먼트 경기가 몇 번 일어나는지 구하시오.

 

풀이

n-1을 반환하면 된다.

n = 8일때, 4 + 2 + 1경기,

n=7일때, 3 + 2 + 1경기...

n = 101일때, 50 + 25 + 13 + 6 + 3 + 2 + 1 = 100경기를 치른다.

 

코드

C++

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