544. 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))).

Solution:

思路:
1 2 3 4 5 6 7 8
因?yàn)槭亲顝?qiáng)和最弱來對(duì)決,其次是次強(qiáng)與次弱對(duì)決躺屁,以此類推可得到:
1-8 2-7 3-6 4-5
那么接下來呢答毫,還是最強(qiáng)與最弱畦粮,次強(qiáng)與次弱這種關(guān)系:
(1-8 4-5) (2-7 3-6)
最后勝者爭奪冠軍
((1-8 4-5) (2-7 3-6))

由于n限定了是2的次方數(shù)绞吁,那么就是可以一直對(duì)半分的哗脖,比如開始有n隊(duì)糊肠,第一拆分為n/2對(duì)匹配,然后再對(duì)半拆悯辙,就是n/2/2琳省,直到拆到n為1停止,而且每次都是首與末配對(duì)笑撞,次首與次末配對(duì).

Time Complexity: O(N) Space Complexity: O(N)

Solution Code:

public class Solution {
    public String findContestMatch(int n) {
        String[] m = new String[n];
        for (int i = 0; i < n; i++) {
            m[i] = String.valueOf(i + 1);
        }

        while (n > 1) {
            for (int i = 0; i < n / 2; i++) {
                m[i] = "(" + m[i] + "," + m[n - 1 - i] + ")";
            }
            n /= 2;
        }
        
        return m[0];
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末岛啸,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子茴肥,更是在濱河造成了極大的恐慌,老刑警劉巖荡灾,帶你破解...
    沈念sama閱讀 211,348評(píng)論 6 491
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件瓤狐,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡批幌,警方通過查閱死者的電腦和手機(jī)础锐,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,122評(píng)論 2 385
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來荧缘,“玉大人皆警,你說我怎么就攤上這事〗卮郑” “怎么了信姓?”我有些...
    開封第一講書人閱讀 156,936評(píng)論 0 347
  • 文/不壞的土叔 我叫張陵,是天一觀的道長绸罗。 經(jīng)常有香客問我意推,道長,這世上最難降的妖魔是什么珊蟀? 我笑而不...
    開封第一講書人閱讀 56,427評(píng)論 1 283
  • 正文 為了忘掉前任菊值,我火速辦了婚禮,結(jié)果婚禮上育灸,老公的妹妹穿的比我還像新娘腻窒。我一直安慰自己,他們只是感情好磅崭,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,467評(píng)論 6 385
  • 文/花漫 我一把揭開白布儿子。 她就那樣靜靜地躺著,像睡著了一般绽诚。 火紅的嫁衣襯著肌膚如雪典徊。 梳的紋絲不亂的頭發(fā)上杭煎,一...
    開封第一講書人閱讀 49,785評(píng)論 1 290
  • 那天,我揣著相機(jī)與錄音卒落,去河邊找鬼羡铲。 笑死,一個(gè)胖子當(dāng)著我的面吹牛儡毕,可吹牛的內(nèi)容都是我干的也切。 我是一名探鬼主播,決...
    沈念sama閱讀 38,931評(píng)論 3 406
  • 文/蒼蘭香墨 我猛地睜開眼腰湾,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼雷恃!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起费坊,我...
    開封第一講書人閱讀 37,696評(píng)論 0 266
  • 序言:老撾萬榮一對(duì)情侶失蹤倒槐,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后附井,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體讨越,經(jīng)...
    沈念sama閱讀 44,141評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,483評(píng)論 2 327
  • 正文 我和宋清朗相戀三年永毅,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了把跨。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,625評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡沼死,死狀恐怖着逐,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情意蛀,我是刑警寧澤耸别,帶...
    沈念sama閱讀 34,291評(píng)論 4 329
  • 正文 年R本政府宣布,位于F島的核電站浸间,受9級(jí)特大地震影響太雨,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜魁蒜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,892評(píng)論 3 312
  • 文/蒙蒙 一囊扳、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧兜看,春花似錦锥咸、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,741評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至弧轧,卻和暖如春雪侥,著一層夾襖步出監(jiān)牢的瞬間碗殷,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,977評(píng)論 1 265
  • 我被黑心中介騙來泰國打工速缨, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留锌妻,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 46,324評(píng)論 2 360
  • 正文 我出身青樓旬牲,卻偏偏與公主長得像仿粹,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子原茅,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,492評(píng)論 2 348