Cluster Process Scheduling
You are a scheduler for a computing cluster. You need to assign nProcesses distinct processes to nIntervals available time slots.
A valid schedule must follow one critical rule: no single process can be assigned to two consecutive time slots.
Your task is to calculate the total number of valid scheduling arrangements. Since the answer can be very large, return the result modulo $10⁹ + 7$.
Example 1
Input
nProcesses = 2, nIntervals = 3Output
2Explanation
Let the processes be A and B. The possible schedules for 3 slots are: - (A, B, A) - (B, A, B) Schedules like (A, A, B) are invalid because process A appears in consecutive slots.
Example 2
Input
nProcesses = 3, nIntervals = 2Output
6Explanation
With 3 processes (A, B, C) and 2 slots, any pair where the first and second processes are different is valid: (A,B), (A,C), (B,A), (B,C), (C,A), (C,B).
Example 3
Input
nProcesses = 1, nIntervals = 2Output
0Explanation
With only 1 process, it must be used in the first slot. To fill the second slot, we would have to use the same process again, which violates the consecutive rule. Thus, 0 valid schedules.
Constraints
1 <= nIntervals <= 10⁹1 <= nProcesses <= 10⁹- Result should be modulo $10⁹ + 7$.
Cluster Process Scheduling Editorial
Overview
The problem asks us to count the number of ways to assign nProcesses to nIntervals such that no two adjacent intervals contain the same process. This is a classic combinatorics problem that can be solved by considering the number of choices available for each time slot.
For the first time slot, we have a free choice of any of the nProcesses. For every subsequent slot, we can choose any process except the one used in the immediately preceding slot. This ensures the "no consecutive" rule is satisfied.
Brute Force
A brute force approach would involve using recursion or backtracking to explore every possible valid sequence of processes. We would try all nProcesses for the first slot, and for each following slot, we would try all processes that are not equal to the previous one.
While this correctly counts the schedules, the number of valid schedules grows exponentially, and a search space of $O(P \cdot (P-1)^{I-1})$ is far too large for the given constraints where nIntervals can be $10^9$.
def countValidSchedules(nProcesses: int, nIntervals: int) -> int:
MOD = 10**9 + 7
def backtrack(current_interval, last_process):
if current_interval == nIntervals:
return 1
count = 0
for p in range(nProcesses):
if p != last_process:
count = (count + backtrack(current_interval + 1, p)) % MOD
return count
# Start with -1 as last_process to allow any process in the first slot
return backtrack(0, -1)
Time complexity: O(nProcesses^nIntervals) — Each of the nIntervals can theoretically branch into nProcesses choices, leading to exponential growth. Space complexity: O(nIntervals) — The recursion stack depth is equal to the number of intervals.
Optimal / Best Approach
The optimal approach utilizes the fundamental counting principle. We can break down the decision-making process slot by slot:
- First Slot: We have nProcesses choices.
- Second Slot: We must pick a process different from the first. This leaves us with (nProcesses - 1) choices.
- Third Slot: We must pick a process different from the second. Even though we could technically reuse the process from the first slot, the only restriction is the second slot. Thus, we again have (nProcesses - 1) choices.
- $i$-th Slot: For any slot from $2$ to $n$, we always have (nProcesses - 1) choices.
The total number of ways is the product of these choices: $$Total = nProcesses \times (nProcesses - 1)^{nIntervals - 1}$$
Since $nIntervals$ is large, we use Modular Exponentiation (the pow function in Python) to calculate $(nProcesses - 1)^{nIntervals - 1} \pmod{10^9 + 7}$ in logarithmic time.
def countValidSchedules(nProcesses: int, nIntervals: int) -> int:
if nIntervals == 0:
return 0
if nProcesses == 0:
return 0
if nProcesses == 1:
# If there is 1 process, it can only fill 1 interval.
# More than 1 interval forces a consecutive repeat.
return 1 if nIntervals == 1 else 0
MOD = 10**9 + 7
# Formula: nProcesses * (nProcesses - 1)^(nIntervals - 1)
# Use pow(base, exp, mod) for efficient calculation
term1 = nProcesses % MOD
term2 = pow(nProcesses - 1, nIntervals - 1, MOD)
return (term1 * term2) % MOD
Time complexity: O(log nIntervals) — Modular exponentiation takes logarithmic time relative to the exponent. Space complexity: O(1) — We only store a few integer variables regardless of the input size.
Edge Cases
- nProcesses = 1: If there is more than one interval, it is impossible to avoid consecutive duplicates. The result is
1ifnIntervals == 1, else0. - nIntervals = 1: The answer is simply
nProcesses, as any single process choice is valid. - Large Inputs: The use of
pow(a, b, m)is critical here to prevent integer overflow and to handle the $10^9$ constraints within the time limit.
Fast
Comments on the solution
No comments on the solution yet. Start a thread about the editorial or your own approach.
No comments yet for Cluster Process Scheduling. Be the first to start the thread.
No submissions recorded for this problem yet. Use Submit in the editor — each judge run is stored here.
Output
Input
Expected
Your output
Run to see your output.