[LeetCode] Output Contest Matches 输出比赛匹配对
During the NBA playoffs, we always arrange the rather strong team to play with the rather weak team, like make the rank 1 team play with the rank nth team, which is a good strategy to make the contest more interesting. Now, you're given n teams, you need to output their final contest matches in the form of a string.
The n teams are given in the form of positive integers from 1 to n, which represents their initial rank. (Rank 1 is the strongest team and Rank n is the weakest team.) We'll use parentheses('(', ')') and commas(',') to represent the contest team pairing - parentheses('(' , ')') for pairing and commas(',') for partition. During the pairing process in each round, you always need to follow the strategy of making the rather strong one pair with the rather weak one.
Example 1:
Input: 2
Output: (1,2)
Explanation:
Initially, we have the team 1 and the team 2, placed like: 1,2.
Then we pair the team (1,2) together with '(', ')' and ',', which is the final answer.
Example 2:
Input: 4
Output: ((1,4),(2,3))
Explanation:
In the first round, we pair the team 1 and 4, the team 2 and 3 together, as we need to make the strong team and weak team together.
And we got (1,4),(2,3).
In the second round, the winners of (1,4) and (2,3) need to play again to generate the final winner, so you need to add the paratheses outside them.
And we got the final answer ((1,4),(2,3)).
Example 3:
Input: 8
Output: (((1,8),(4,5)),((2,7),(3,6)))
Explanation:
First round: (1,8),(2,7),(3,6),(4,5)
Second round: ((1,8),(4,5)),((2,7),(3,6))
Third round: (((1,8),(4,5)),((2,7),(3,6)))
Since the third round will generate the final winner, you need to output the answer (((1,8),(4,5)),((2,7),(3,6))).
Note:
- The n is in range [2, 212].
- We ensure that the input n can be converted into the form 2k, where k is a positive integer.
这道题讲的是NBA的季后赛对战顺序,对于一个看了十几年NBA的老粉来说,再熟悉不过了。这种对战顺序是为了避免强强之间过早对决,从而失去比赛的公平性,跟欧冠欧联那种八强就开始随机抽签匹配有本质上的区别。NBA的这种比赛机制基本弱队很难翻身,假如你是拿到最后一张季后赛门票进的,那么一上来就干联盟第一,肯定凶多吉少,很有可能就被横扫了。但是偶尔也会出现黑八的情况,但都是极其少见的,毕竟像勇士这么叼的球队毕竟不多。好了,不闲扯了,来做题吧。我们就拿NBA这种八个球队的情况来分析吧,八只球队的排名是按常规赛胜率来排的:
1 2 3 4 5 6 7 8
因为是最强和最弱来对决,其次是次强与次弱对决,以此类推可得到:
1-8 2-7 3-6 4-5
那么接下来呢,还是最强与最弱,次强与次弱这种关系:
(1-8 4-5) (2-7 3-6)
最后胜者争夺冠军
((1-8 4-5) (2-7 3-6))
这样一分析是不是就清楚了呢,由于n限定了是2的次方数,那么就是可以一直对半分的,比如开始有n队,第一拆分为n/2对匹配,然后再对半拆,就是n/2/2,直到拆到n为1停止,而且每次都是首与末配对,次首与次末配对,这样搞清楚了规律,代码应该就不难写了吧,参见代码如下:
解法一:
class Solution {
public:
string findContestMatch(int n) {
vector<string> v;
for (int i = ; i <= n; ++i) v.push_back(to_string(i));
while (n > ) {
for (int i = ; i < n / ; ++i) {
v[i] = "(" + v[i] + "," + v[n - i - ] + ")";
}
n /= ;
}
return v[];
}
};
下面这种方法是递归的写法,解题思路跟上面没有区别,参见代码如下:
解法二:
class Solution {
public:
string findContestMatch(int n) {
vector<string> v;
for (int i = ; i <= n; ++i) v.push_back(to_string(i));
helper(n, v);
return v[];
}
void helper(int n, vector<string>& v) {
if (n == ) return;
for (int i = ; i < n; ++i) {
v[i] = "(" + v[i] + "," + v[n - i - ] + ")";
}
helper(n / , v);
}
};
参考资料:
https://discuss.leetcode.com/topic/83454/java-10-lines/2
https://discuss.leetcode.com/topic/83457/c-java-clean-code
https://discuss.leetcode.com/topic/83460/java-recursive-solution
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Output Contest Matches 输出比赛匹配对的更多相关文章
- [LeetCode] 544. Output Contest Matches 输出比赛匹配对
During the NBA playoffs, we always arrange the rather strong team to play with the rather weak team, ...
- LeetCode Output Contest Matches
原题链接在这里:https://leetcode.com/problems/output-contest-matches/description/ 题目: During the NBA playoff ...
- 【leetcode】544. Output Contest Matches
原题 During the NBA playoffs, we always arrange the rather strong team to play with the rather weak te ...
- 【LeetCode】544. Output Contest Matches 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
- LeetCode 544----Output Contest Matches
During the NBA playoffs, we always arrange the rather strong team to play with the rather weak team, ...
- LeetCode Weekly Contest 24
1, 543. Diameter of Binary Tree 维护左右子树的高度,同时更新结果,返回以该节点结束的最大长度.递归调用. /** * Definition for a binary t ...
- BZOJ1612: [Usaco2008 Jan]Cow Contest奶牛的比赛
1612: [Usaco2008 Jan]Cow Contest奶牛的比赛 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 645 Solved: 433 ...
- Bzoj 1612: [Usaco2008 Jan]Cow Contest奶牛的比赛 传递闭包,bitset
1612: [Usaco2008 Jan]Cow Contest奶牛的比赛 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 891 Solved: 590 ...
- BZOJ 1612: [Usaco2008 Jan]Cow Contest奶牛的比赛( floyd )
对于第 i 头牛 , 假如排名比它高和低的数位 n - 1 , 那么他的 rank 便可以确定 . floyd -------------------------------------------- ...
随机推荐
- 数据库(Mongodb)
1.MongoClient()函数 In [8]: import pymongo In [9]: con = pymongo.MongoClient('localhost') #建立连接 In [10 ...
- android中shape属性大全
出处:http://kofi1122.blog.51cto.com/2815761/521605
- [Java反射机制]用反射改进简单工厂模式设计
如果做开发的工作,工厂设计模式大概都已经深入人心了,比较常见的例子就是在代码中实现数据库操作类,考虑到后期可能会有数据库类型变换或者迁移,一般都会对一个数据库的操作类抽象出来一个接口,然后用工厂去获取 ...
- CountDownLatch 源码解析—— countDown()
上一篇文章从源码层面说了一下CountDownLatch 中 await() 的原理.这篇文章说一下countDown() . public void countDown() { //CountDow ...
- 第六周PTA作业
第一题: #include<stdio.h> int main () { float a,b; scanf("%f %f\n",&a,&b); floa ...
- Alpha冲刺Day8
Alpha冲刺Day8 一:站立式会议 今日安排: 经过为期5天的冲刺,基本完成企业人员模块的开发.因第三方机构与企业存在委托的关系.第三方人员对于风险的自查.风险列表的展示以及自查风险的统计展示(包 ...
- 数据故障的恢复-MSSQL ndf文件大小变为0 KB恢复过程
一.故障描述 成都某客户,存储损坏,数据库崩溃.重组存储,恢复数据库文件,发现有四个ndf文件大小变为0 KB.数据库大小约80TB.数据库中有1223个文件,数据库每10天生成一个NDF文件,每个N ...
- JAVA_SE基础——14.循环结构语句
建议有些基础的同学阅读,0基础可能会有些困难(最好看正文配合基础课本的例子) 所谓循环语句主要就是在满足条件的情况下反复执行某一个操作.Java提供了3种常用的循环语句,分别为for循环语句.whil ...
- java之多态详解
前言 什么叫多态?多态就是一种事物可以有多种表现形式 多态三要素 1.被动方必须有继承关系 2.子类一般都要重写父类方法 3.必须将主动方的功能函数的参数设置为 被动方父类的类型 举个例子司机开车 假 ...
- nat和napt技术
私网IP地址是指内部网络或主机的IP地址,公网IP地址是指在因特网上全球唯一的IP地址. RFC 1918为私有网络预留出了三个IP地址块,如下: A类:10.0.0.0-10.255.255.255 ...