Count the Number of Infection Sequences - Complete Solution Guide
Count the Number of Infection Sequences is LeetCode problem 2954, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You are given an integer n and an array sick sorted in increasing order, representing positions of infected people in a line of n people. At each step, one uninfected person adjacent to an infected person gets infected. This process continues until everyone is infected. An infection sequence is the order in which uninfected people become infected, excluding those initially infected. Return the number of different infection sequences possible, modulo 10 9 +7 . Example 1: Input: n = 5, sick = [0,4
Detailed Explanation
The problem describes a scenario where people in a line of `n` individuals become infected. Initially, some people are infected (their positions are given in the `sick` array, sorted in increasing order). At each step, an uninfected person adjacent to an infected person gets infected. The goal is to count the number of different *infection sequences* possible, modulo 10^9 + 7. An infection sequence is the order in which *uninfected* people become infected. The initial sick individuals are not part of the sequence.
Solution Approach
The solution involves calculating the number of ways to arrange the infection sequence. It breaks down into the following steps: 1. Calculate the number of uninfected people. 2. Calculate the length of each group of consecutive uninfected people between the infected people. 3. Calculate the number of ways to arrange the infection sequence using combinations. This involves calculating factorials and their modular inverses. 4. Account for the multiple ways groups of infected people can be ordered based on which side they are infected from using the power of 2.
Step-by-Step Algorithm
- Step 1: Calculate `m`, the number of uninfected people: `m = n - k`, where `k` is the number of initially infected people.
- Step 2: Calculate factorials modulo MOD for numbers up to `n` and store them in the `fact` array. This is a precomputation step to avoid repeated calculations.
- Step 3: Calculate the number of ways to infect the individuals to the left of the first infected person `sick[0]`. This contributes to the number of combinations.
- Step 4: Iterate through the `sick` array from the second element. Calculate the length `l_i` of the uninfected gap between `sick[i]` and `sick[i-1]`: `l_i = sick[i] - sick[i-1] - 1`.
- Step 5: If `l_i > 0`, update the combinations term by dividing by `fact[l_i]` and multiply the internal term by `power(2, l_i - 1)` to account for the possible infection order in either direction. Note, the groups at the beginning and end can only be infected in one direction.
- Step 6: Calculate the number of ways to infect the individuals to the right of the last infected person `sick[k-1]`. This again contributes to combinations.
- Step 7: Return the result of `(combinations_term * internal_term) % MOD`.
Key Insights
- Insight 1: The problem is fundamentally a combinatorics problem. The order in which sections of uninfected people between sick people become infected matters.
- Insight 2: The uninfected people can be viewed as separate groups between the initial infected people. Each group can be infected from either end, leading to 2^(group_size - 1) possibilities within each group (except for the groups at the start and end which have only one way to be infected.)
- Insight 3: We need to calculate combinations (n choose k) where 'n' is the total number of uninfected people, and 'k' represents the sizes of the gaps. Using modular arithmetic to prevent overflow and Fermat's Little Theorem to calculate modular inverses is crucial.
- Insight 4: Since the people can be infected in either direction it creates multiple infection sequences, we need to calculate the number of ways each group in between the sick people can be infected from either side. For the groups at the start and end, there is only one way to infect them.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: Array, Math, Combinatorics.
Companies
Asked at: SAP, Tekion.