Output Contest Matches - Problem
Imagine organizing the NBA playoffs where excitement comes from strategic team pairings! ๐
You're given n teams ranked from 1 (strongest) to n (weakest). Your task is to create tournament brackets following this strategy: always pair the strongest remaining team with the weakest remaining team.
Goal: Return the final contest matches as a properly formatted string using:
()parentheses for team pairings,commas to separate different matchups
Example: With 4 teams [1,2,3,4], team 1 pairs with team 4, and team 2 pairs with team 3, resulting in "((1,4),(2,3))"
The pairing continues recursively until only one final match remains!
Input & Output
example_1.py โ Small Tournament
$
Input:
n = 4
โบ
Output:
"((1,4),(2,3))"
๐ก Note:
Teams 1,2,3,4 get paired as: strongest(1) with weakest(4), second-strongest(2) with second-weakest(3), resulting in matches (1,4) and (2,3). These two matches form the final bracket.
example_2.py โ Larger Tournament
$
Input:
n = 8
โบ
Output:
"(((1,8),(4,5)),((2,7),(3,6)))"
๐ก Note:
First round: (1,8), (2,7), (3,6), (4,5). Second round: ((1,8),(4,5)) and ((2,7),(3,6)). Final bracket combines these two semi-final matches.
example_3.py โ Minimum Case
$
Input:
n = 2
โบ
Output:
"(1,2)"
๐ก Note:
With only 2 teams, there's just one match: strongest team 1 vs weakest team 2.
Constraints
- 2 โค n โค 212
- n is a power of 2 (ensures perfect tournament brackets)
- Teams are numbered from 1 to n where 1 is strongest
Visualization
Tap to expand
Understanding the Visualization
1
Initial Setup
Start with n teams ranked 1 (strongest) to n (weakest)
2
Strategic Pairing
Pair team 1 with team n, team 2 with team (n-1), etc.
3
Build Brackets
Create match strings and repeat until final bracket
4
Final Result
Return the complete tournament bracket structure
Key Takeaway
๐ฏ Key Insight: Tournament brackets naturally follow a recursive structure where each round reduces participants by half, making recursion the perfect solution approach!
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code