[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 -------------------------------------------- ...
随机推荐
- 随记PC-win7 64位系统网络连接状态一直转圈、等待状态的异常解决方案
各位看官好~ 最近电脑也做了下升级,入手个士必得360G的SSD来玩玩,顺便也下个新系统,看看有什么区别,想想顺便升级下系统也是好的,就开始了装机,装系统的路程~~~~~~ 好了不说废话,直接进入主题 ...
- jmeter常见问题汇总
Aggregate Report 是 JMeter 常用的一个 Listener,中文被翻译为"聚合报告".今天再次有同行问到这个报告中的各项数据表示什么意思,顺便在这里公布一下, ...
- JAVA多线程实现和应用总结
1.JAVA多线程实现方式JAVA多线程实现方式主要有三种:继承Thread类.实现Runnable接口.使用ExecutorService.Callable.Future实现有返回结果的多线程.其中 ...
- 网站加速与Linux服务器防护
网站加速方面 1. Nginx 配置 gzip 压缩 开启nginx gzip压缩后,网页.css.js等静态资源的大小会大大的减少,从而可以节约大量的带宽,提高传输效率,给用户快的体验.虽然会消耗c ...
- php设计模式八-----装饰器模式
1.介绍: 装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创建了一个装饰 ...
- 敏捷冲刺每日报告——Day3
1.情况简述 Alpha阶段第一次Scrum Meeting 敏捷开发起止时间 2017.10.27 00:00 -- 2017.10.28 00:00 讨论时间地点 2017.10.27晚9:30, ...
- Ubuntu登陆密码忘记
在VMware中安装了Ubuntu 10.04,经过了一段时间,再次登录的时候居然进不去了, 一开始不知道怎样在虚拟机中进入到Grub启动界面,网上搜索了一番,按照以下步骤重新为用户设定了新密码. 重 ...
- [JCIP笔记] (三)如何设计一个线程安全的对象
在当我们谈论线程安全时,我们在谈论什么中,我们讨论了怎样通过Java的synchronize机制去避免几个线程同时访问一个变量时发生问题.忧国忧民的Brian Goetz大神在多年的开发过程中,也悟到 ...
- webapi 使用Autofac 开发经历
2018/4/6 号 早上五点..被手机震动吵醒. 之后直接打开电脑,打算再加强下我自己的webapi这套东西. 虽然三年的工作经验接触了N多框架和各种风格的开发方式,但是让我自己来搞一套实在不会搞, ...
- python小练习之一
下面的练习本身不难,比如打印1到10,计算1+2+3+...+100 ,最后一个是计算 1-2+3-4...-100 用了类的方法实现 用了列表生成器 用"高级"一丢丢的写法来实现 ...