Count the Number of Infection Sequences - Problem
Pandemic Spread Simulation
Imagine a pandemic spreading through a line of
The infection spreads with these rules:
• At each time step, exactly one uninfected person adjacent to an infected person becomes infected
• This continues until everyone is infected
• An infection sequence is the specific order in which the uninfected people become infected
Goal: Calculate how many different infection sequences are possible.
Example: If
Return the answer modulo
Imagine a pandemic spreading through a line of
n people, where some individuals are initially infected. You're given an integer n representing the total number of people and an array sick (sorted in increasing order) representing the positions of people who are already infected.The infection spreads with these rules:
• At each time step, exactly one uninfected person adjacent to an infected person becomes infected
• This continues until everyone is infected
• An infection sequence is the specific order in which the uninfected people become infected
Goal: Calculate how many different infection sequences are possible.
Example: If
n=5 and sick=[0,4] (positions 0 and 4 are infected), the infection can spread inward from both ends. Person at position 1 or 3 could be infected first, leading to different sequences.Return the answer modulo
109 + 7. Input & Output
example_1.py — Basic Case
$
Input:
n = 5, sick = [0, 4]
›
Output:
4
💡 Note:
People at positions 0 and 4 are initially infected. The infection can spread inward. Possible sequences: [1,2,3], [1,3,2], [3,2,1], [3,1,2] - total of 4 different sequences.
example_2.py — Single Group
$
Input:
n = 4, sick = [1]
›
Output:
6
💡 Note:
Only position 1 is infected initially. Infection can spread left to 0 and right to 2,3. The sequences are different orderings of [0,2,3], giving us 3! = 6 possibilities.
example_3.py — All Infected
$
Input:
n = 3, sick = [0, 1, 2]
›
Output:
1
💡 Note:
Everyone is already infected, so there's only 1 way (no additional infections needed).
Constraints
- 1 ≤ n ≤ 105
- 1 ≤ sick.length ≤ n
- 0 ≤ sick[i] ≤ n - 1
- sick is sorted in increasing order
- All values in sick are distinct
Visualization
Tap to expand
Understanding the Visualization
1
Identify Groups
Split uninfected people into isolated groups between infected positions
2
Group Independence
Each group spreads independently - use multiplication principle
3
Calculate Coefficients
For each group, calculate how many ways it can be infected using combinatorics
4
Combine Results
Multiply all group possibilities to get total sequences
Key Takeaway
🎯 Key Insight: Transform the infection simulation problem into a combinatorial counting problem by recognizing that isolated groups of uninfected people spread independently, allowing us to use mathematical formulas instead of expensive simulation.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code