Output Contest Matches Problem
Description
LeetCode Problem 544.
During the NBA playoffs, we always set the rather strong team to play with the rather weak team, like making the rank 1 team play with the rank n^th team, which is a good strategy to make the contest more interesting.
Given n teams, return their final contest matches in the form of a string.
The n teams are labeled from 1 to n, which represents their initial rank (i.e., Rank 1 is the strongest team and Rank n is the weakest team).
We will use parentheses '(', and ')' and commas ',' to represent the contest team pairing. We use the parentheses for pairing and the 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: n = 4
Output: "((1,4),(2,3))"
Explanation:
In the first round, we pair the team 1 and 4, the teams 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 2:
Input: n = 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))).
Constraints:
n == 2^xwherexin in the range[1, 12].
Solution
Approach: Simulate the Rounds
Keep a list with one string per remaining "team", ordered from strongest to weakest. At the start, team i is just the string "i".
In each round, pair the strongest with the weakest, the second strongest with the second weakest, and so on: entry i becomes "(" + teams[i] + "," + teams[n-1-i] + ")" for i < n/2. Then halve n and repeat until one string is left.
Why does this keep the right order for the next round? We assume the stronger team always wins, so the winner of pair i is the team at position i, the stronger one. The new list is therefore again ordered from strongest to weakest, and the next round can pair its ends in the same way. Writing the pairs into the first half of the same list works because entry i only reads entries i and n-1-i, and n-1-i is in the second half, which is never written in this round.
For n = 8: the first round gives (1,8), (2,7), (3,6), (4,5); the second pairs the first with the last and the middle two: ((1,8),(4,5)), ((2,7),(3,6)); the third joins those two.
Complexity: There are log n rounds, and each round copies strings whose total length is at most the length L of the final answer (L = O(n log n), since team numbers have up to log n digits). That is O(L log n) time and O(L) space.
class Solution:
def findContestMatch(self, n: int) -> str:
teams = [str(i) for i in range(1, n + 1)] # strongest first
while n > 1:
# the i-th strongest pairs with the i-th weakest; the pairs stay in strength order
for i in range(n // 2):
teams[i] = "(" + teams[i] + "," + teams[n - 1 - i] + ")"
n //= 2
return teams[0]