← 返回 bloomberg 的题目列表Round-Robin Tournament Schedule
类型:qbank
Given `n` teams, enumerate all valid round-robin schedules where each round pairs every team with a different opponent, every pair plays exactly once across all rounds, and each team plays every other team. Trickier when `n` is odd (a bye must be modeled).
Requirements
Given an even integer n, return all valid round-robin schedules of n - 1 rounds. Each round is a list of n / 2 disjoint pairs; across all rounds every unordered pair (i, j) with i < j appears exactly once.
For odd n, one team sits out (bye) each round — clarify whether the interviewer wants this modeled.
Function signature:
List<List<List<int[]>>> allRoundRobinSchedules(int n)
Examples
n = 4
One valid schedule:
[[(1,2), (3,4)],
[(1,3), (2,4)],
[(1,4), (2,3)]]
Notes
The backtracking skeleton: maintain a played[i][j] matrix. For each round, recursively pick a partner for the lowest-index unmatched team, mark that pair played and matched-for-the-round, recurse to the next unmatched team. When the round is filled, recurse to the next round; when all n - 1 rounds are filled, emit the schedule.
Pinning the lowest-index unmatched team as the "left" side of each pair eliminates duplicate schedules from symmetric pair re-orderings.
Time complexity is hard to bound tightly; the count of distinct round-robin schedules grows super-exponentially. Interviewers usually accept the backtracking implementation as the answer and discuss only one schedule as a follow-up if time matters.
For odd n, the standard trick is to add a phantom team n + 1 whose opponent in each round is the team on bye. Then run the even-n algorithm and strip the phantom matches.
A constructive (non-enumeration) algorithm exists for one valid schedule: the "circle method" rotates n - 1 teams around a fixed pivot. Mention as the O(n^2)-per-schedule answer when the interviewer asks for just one.
Preparation
Implement the backtracking algorithm for n = 4 and n = 6 and verify the count of distinct schedules matches a small reference (1 for n = 4 up to symmetry, 6 for n = 6).
Derive the circle-method construction on paper; this is a common follow-up when the interviewer caps the count to one schedule.
Be explicit about the symmetry-breaking step (pin the lowest unmatched team) — without it, the enumeration produces duplicates and interviewers will ask why.