Skip to main content

Generate Schedule - Solution & Explanation

MediumArrayMathGreedy3 min readAsked at: Oracle
Practice this problem

Problem Statement

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 day i.
  • 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​​​​​​​

Approach Overview

Problem 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

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force SimulationO(n²)O(n)Useful for understanding constraints or small inputs
Greedy + Math DistributionO(n)O(n)Best general solution when schedule structure can be computed mathematically

Video Solution

Generate Schedule | LeetCode 3680 | Best Solution ExplainedSanyam IIT Guwahati1,087 views views

Watch 5 more video solutions →

Frequently Asked Questions

Is Generate Schedule easy or hard?
Generate Schedule is typically considered a medium difficulty problem. The implementation itself is straightforward once you identify the greedy distribution pattern, but recognizing that the schedule structure can be computed mathematically is the main challenge.
Generate Schedule Python/Java solution
Most implementations follow the same idea: compute the number of items per slot, initialize an array for the result, and fill it sequentially using greedy placement. The logic translates directly to Python lists, Java arrays, or C++ vectors with identical O(n) complexity.
How to solve Generate Schedule in O(n)?
Compute the required distribution of elements across the schedule using a mathematical formula such as ceiling division or fixed slot capacity. Then iterate once through the input and place each element directly into its assigned position. This avoids nested scans and keeps the algorithm linear.
What is the best approach for Generate Schedule?
The optimal approach uses a greedy strategy combined with simple math to determine how many elements belong in each slot of the schedule. Instead of repeatedly checking placements, you compute the distribution first and then fill the schedule in a single pass. This reduces the runtime to O(n) with O(n) extra space.
Is Generate Schedule asked at Google/Amazon/Meta?
Scheduling and distribution problems using greedy reasoning frequently appear in interviews at companies like Amazon, Google, and Meta. Variations typically focus on balancing tasks, arranging events, or constructing valid sequences under constraints.
What data structure is used in Generate Schedule?
The primary data structure is an array used to store the resulting schedule. The algorithm may also use counters or index pointers to track slot boundaries. Combined with greedy placement and simple math calculations, this keeps the implementation efficient.
What is the time complexity of Generate Schedule?
The optimal greedy solution runs in O(n) time because each element is processed once when constructing the schedule array. Space complexity is O(n) to store the resulting schedule. A naive simulation approach can degrade to O(n²) due to repeated validation checks.

Ready to solve this problem?

Practice Generate Schedule with our built-in code editor and test cases.

Practice on FleetCode