Generate Schedule Problem
Description
LeetCode Problem 3680.
You are given an integer n representing n teams. You are asked to generate a schedule such that:
- Each team plays every other team exactly twice: once at home and once away.
- There is exactly one match per day; the schedule is a list of consecutive days and
schedule[i]is the match on dayi. - No team plays on consecutive days.
Return a 2D integer array schedule, where schedule[i][0] represents the home team and schedule[i][1] represents the away team. If multiple schedules meet the conditions, return any one of them.
If no schedule exists that meets the conditions, return an empty array.
Example 1:
Input: n = 3
Output: []
Explanation:
Since each team plays every other team exactly twice, a total of 6 matches need to be played: [0,1],[0,2],[1,2],[1,0],[2,0],[2,1].
It's not possible to create a schedule without at least one team playing consecutive days.
Example 2:
Input: n = 5
Output: [[0,1],[2,3],[0,4],[1,2],[3,4],[0,2],[1,3],[2,4],[0,3],[1,4],[2,0],[3,1],[4,0],[2,1],[4,3],[1,0],[3,2],[4,1],[3,0],[4,2]]
Explanation:
Since each team plays every other team exactly twice, a total of 20 matches need to be played.
The output shows one of the schedules that meet the conditions. No team plays on consecutive days.
Constraints:
2 <= n <= 50
Solution
Approach: Greedy Construction
For n <= 4 no schedule exists. With n = 2 or 3 the matches cannot avoid sharing a team, and with n = 4 every day uses two of the four teams, so the next day must be played by the other two. That forces the same pairs to alternate, and the schedule gets stuck before all 12 matches are used. For n >= 5 a schedule exists.
To build one, play the matches one day at a time. Keep how many matches each team still has to play. On each day, among the unplayed matches whose two teams did not play on the previous day, choose the one whose two teams have the most matches left (the first one found when there is a tie). Giving priority to the busiest teams keeps them from being left with only conflicting matches at the end. This produces a valid schedule for every n from 5 to 50. The code runner checks each answer instead of comparing it to one fixed schedule, since any valid schedule is accepted.
Complexity: O(n^4) time (n^2 days, each scanning n^2 pairs) and O(n^2) space.
class Solution:
def generateSchedule(self, n: int) -> List[List[int]]:
if n < 5: # n <= 4 has no valid schedule
return []
left = [[True] * n for _ in range(n)] # left[a][b]: match a (home) vs b (away) not played yet
todo = [2 * (n - 1)] * n # matches each team still has to play
ans = []
last = (-1, -1)
for _ in range(n * (n - 1)):
best = None
for a in range(n):
if a in last:
continue
for b in range(n):
if b == a or b in last or not left[a][b]:
continue
key = (todo[a] + todo[b], -a, -b) # prefer teams with the most matches left
if best is None or key > best:
best = key
a, b = -best[1], -best[2]
left[a][b] = False
todo[a] -= 1
todo[b] -= 1
ans.append([a, b])
last = (a, b)
return ans