#3680

Generate Schedule

Medium
ArrayMathGreedyHash MapArray
LeetCode ↗

Approaches

Brute ForceOptimal
Complexity Comparison
Brute ForceOptimal Solution
Time
O(n²)
O(n²)
Space
O(1)
O(n)
💡

Intuition

Time O(n²)Space O(n)

Use a round-robin scheduling method to ensure no team plays on consecutive days while covering all matches efficiently.

⚙️

Algorithm

3 steps
  1. 1Step 1: Create a list to hold the matches.
  2. 2Step 2: Loop through each team and schedule matches while skipping the previous day's teams.
  3. 3Step 3: Return the constructed schedule.
solution.py11 lines
1def generate_schedule(n):
2    if n < 3:
3        return []
4    schedule = []
5    for i in range(n):
6        for j in range(1, n):
7            home = (i + j) % n
8            away = (i + n - j) % n
9            if home != away:
10                schedule.append([home, away])
11    return schedule

Complexity note: The algorithm runs in quadratic time due to nested loops but uses linear space to store the schedule.

  • 1A round-robin approach ensures all teams play without consecutive matches.
  • 2The problem is infeasible for n < 3 due to the requirement of home and away games.

Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.