Notice
250x250
Recent Posts
Recent Comments
Link
넘치게 채우기
[LeetCode] 1688. Count of Matches in Tournament 본문
728x90
반응형
https://leetcode.com/problems/count-of-matches-in-tournament/description/
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
반응형
'PS > LeetCode' 카테고리의 다른 글
[Leetcode] 1903. Largest Odd Number in String (0) | 2023.12.07 |
---|---|
[LeetCode] 1716. Calculate Money in Leetcode Bank (0) | 2023.12.06 |
[LeetCode] 2264. Largest 3-Same-Digit Number in String (0) | 2023.12.04 |
[LeetCode] 1266. Minimum Time Visiting All Points (0) | 2023.12.03 |
[LeetCode] 1160. Find Words That Can Be Formed by Characters (0) | 2023.12.02 |