파비의 매일매일 공부기록

2023.12.06 Today's Challenge 본문

Problem Solving/LeetCode

2023.12.06 Today's Challenge

fabichoi 2023. 12. 6. 23:45

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

 

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

매우 간단한 문제.
다만 base case에 대한 더 깔끔한 처리 방법이 있으면 좋겠음.

class Solution:
    def numberOfMatches(self, n: int) -> int:        
        if n == 1:
            return 0
        if n == 2:
            return 1
        cnt = 0
        while n > 2: 
            if n % 2:
                cnt += (n - 1) // 2
                n = (n - 1) // 2 + 1
            else:
                n = n // 2
                cnt += n
        return cnt + 1
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

2023.12.08 Today's Challenge  (0) 2023.12.08
2023.12.07 Today's Challenge  (0) 2023.12.07
2023.12.05 Today's Challenge  (0) 2023.12.05
2023.12.04 Today's Challenge  (1) 2023.12.04
2023.12.03 Today's Challenge  (0) 2023.12.03
Comments