1688. 比赛中的配对次数
1688. 比赛中的配对次数
题目
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 / 2matches are played, andn / 2teams 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) / 2matches are played, and(n - 1) / 2 + 1teams advance to the next round.
Return the number of matches played in the tournament until a winner is decided.
Example 1:
Input: n = 7
Output: 6
Explanation: Details of the tournament:
- 1st Round: Teams = 7, Matches = 3, and 4 teams advance.
- 2nd Round: Teams = 4, Matches = 2, and 2 teams advance.
- 3rd Round: Teams = 2, Matches = 1, and 1 team is declared the winner.
Total number of matches = 3 + 2 + 1 = 6.
Example 2:
Input: n = 14
Output: 13
Explanation: Details of the tournament:
- 1st Round: Teams = 14, Matches = 7, and 7 teams advance.
- 2nd Round: Teams = 7, Matches = 3, and 4 teams advance.
- 3rd Round: Teams = 4, Matches = 2, and 2 teams advance.
- 4th Round: Teams = 2, Matches = 1, and 1 team is declared the winner.
Total number of matches = 7 + 3 + 2 + 1 = 13.
Constraints:
1 <= n <= 200
题目大意
给你一个整数 n ,表示比赛中的队伍数。比赛遵循一种独特的赛制:
- 如果当前队伍数是 偶数 ,那么每支队伍都会与另一支队伍配对。总共进行
n / 2场比赛,且产生n / 2支队伍进入下一轮。 - 如果当前队伍数为 奇数 ,那么将会随机轮空并晋级一支队伍,其余的队伍配对。总共进行
(n - 1) / 2场比赛,且产生(n - 1) / 2 + 1支队伍进入下一轮。
返回在比赛中进行的配对次数,直到决出获胜队伍为止。
示例 1:
输入: n = 7
输出: 6
解释: 比赛详情:
- 第 1 轮:队伍数 = 7 ,配对次数 = 3 ,4 支队伍晋级。
- 第 2 轮:队伍数 = 4 ,配对次数 = 2 ,2 支队伍晋级。
- 第 3 轮:队伍数 = 2 ,配对次数 = 1 ,决出 1 支获胜队伍。
总配对次数 = 3 + 2 + 1 = 6
示例 2:
输入: n = 14
输出: 13
解释: 比赛详情:
- 第 1 轮:队伍数 = 14 ,配对次数 = 7 ,7 支队伍晋级。
- 第 2 轮:队伍数 = 7 ,配对次数 = 3 ,4 支队伍晋级。
- 第 3 轮:队伍数 = 4 ,配对次数 = 2 ,2 支队伍晋级。
- 第 4 轮:队伍数 = 2 ,配对次数 = 1 ,决出 1 支获胜队伍。
总配对次数 = 7 + 3 + 2 + 1 = 13
提示:
1 <= n <= 200
解题思路
思路一:位运算
模拟比赛过程:
- 使用一个变量
matches记录比赛总数。 - 使用
n表示当前轮的队伍数,不断更新n和matches,直到n = 1时停止。
- 使用一个变量
奇偶处理:
- 如果当前队伍数
n是 偶数:- 所有队伍都可以配对比赛,每场淘汰一支队伍。
- 比赛场数为
n / 2,剩余的队伍数为n / 2。
- 如果当前队伍数
n是 奇数:- 多出的一支队伍直接晋级,其余队伍配对比赛。
- 比赛场数为
Math.floor(n / 2),剩余的队伍数为Math.floor(n / 2) + 1。
- 如果当前队伍数
循环模拟:
- 通过
while (n > 1)不断模拟比赛,每轮更新队伍数和比赛总数,直到只剩下一支队伍。
- 通过
位运算:
n & 1判断n是否是奇数:n & 1 == 1表示n是奇数。n & 1 == 0表示n是偶数。
n >>= 1表示将n右移一位,相当于n = Math.floor(n / 2)。
复杂度分析
- 时间复杂度:
O(1),每次n至少减半,因此时间复杂度为O(log n)。 - 空间复杂度:
O(1),没有使用额外空间。
思路二:公式法
实际上可以通过观察发现,无论队伍数是奇数还是偶数,每淘汰一支队伍都增加一次比赛,总比赛场数永远是初始队伍数减 1。
因此,可以直接返回 n - 1:
复杂度分析
- 时间复杂度:
O(1),直接计算。 - 空间复杂度:
O(1),没有使用额外空间。
代码
位运算
/**
* @param {number} n
* @return {number}
*/
var numberOfMatches = function (n) {
let matches = 0;
while (n > 1) {
// 模拟比赛过程
if (n & (1 == 1)) {
// 奇数情况下,额外晋级一支队伍
matches += 1;
}
n >>= 1; // 等价于 n = Math.floor(n / 2)
matches += n; // 记录淘汰的队伍数
}
return matches; // 返回总比赛数
};
公式法
/**
* @param {number} n
* @return {number}
*/
var numberOfMatches = function (n) {
return n - 1;
};
相关题目
| 题号 | 标题 | 题解 | 标签 | 难度 | 力扣 |
|---|---|---|---|---|---|
| 2549 | 统计桌面上的不同数字 | 数组 哈希表 数学 1+ | 🟢 | 🀄️ 🔗 |
