You are given an integer n representing n teams. You are asked to generate a schedule such that:
schedule[i] is the match on day i.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 <= 50Problem Overview: You need to construct a valid schedule from a set of inputs while respecting ordering or capacity constraints. The goal is not just to check feasibility but to actually build the schedule array that distributes items across slots in a valid way.
Approach 1: Brute Force Simulation (O(n²) time, O(n) space)
The straightforward idea is to simulate the scheduling process step by step. Iterate through the items and try placing each one into the earliest valid slot while checking constraints against previously scheduled elements. Each placement may require scanning existing assignments to ensure the schedule remains valid. This repeated validation leads to quadratic behavior. The approach is simple to reason about and useful for verifying correctness during development, but it becomes slow as the number of elements grows.
Approach 2: Greedy Distribution with Math (O(n) time, O(n) space)
The key insight is that the schedule structure can be determined mathematically before constructing it. Instead of repeatedly checking placements, compute how many elements should appear in each slot using simple arithmetic (for example, evenly distributing items across available positions using ceiling division). Then iterate once through the array and assign each element directly to its computed position. This removes the need for repeated validation and keeps the construction linear.
The greedy aspect comes from always filling the earliest available valid slot. Because the distribution is pre‑computed, every assignment automatically satisfies the constraints. Implementation typically uses a simple Array to store the schedule and pointer indices that move forward as positions are filled.
Conceptually, this mixes greedy placement with small math calculations to determine capacity limits. The schedule itself is represented with an array, making both reads and writes constant time.
Recommended for interviews: Interviewers expect the greedy + math construction. Starting with the brute force simulation shows you understand the scheduling constraints, but recognizing that the distribution can be computed directly demonstrates stronger algorithmic thinking and reduces the runtime to linear complexity.
Solutions for this problem are being prepared.
Try solving it yourself| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Simulation | O(n²) | O(n) | Useful for understanding constraints or small inputs |
| Greedy + Math Distribution | O(n) | O(n) | Best general solution when schedule structure can be computed mathematically |
Generate Schedule | LeetCode 3680 | Best Solution Explained • Sanyam IIT Guwahati • 1,017 views views
Watch 5 more video solutions →Practice Generate Schedule with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor