반응형
https://leetcode.com/problems/count-of-matches-in-tournament/description
반복문을 이용해 로그 시간에 풀 수 있었습니다.
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;
}
};
반응형
'Algorithm' 카테고리의 다른 글
LeetCode 1903. Largest Odd Number in String (1) | 2023.12.07 |
---|---|
LeetCode 1716. Calculate Money in Leetcode Bank (1) | 2023.12.07 |
LeetCode 2264. Largest 3-Same-Digit Number in String (0) | 2023.12.04 |
LeetCode 1266. Minimum Time Visiting All Points (1) | 2023.12.03 |
LeetCode 1160. Find Words That Can Be Formed by Characters (2) | 2023.12.02 |