Count the distinct sequences in which all houses in a line become infected.
Given n houses numbered 0 to n-1 in a straight line and an array infected of houses infected on Day 0, every infected house infects its immediate neighbors (i-1 and i+1) starting the next day. Infection continues until every house that can be infected is infected. Two outcomes are different if the sequence in which houses become infected differs. Return the number of distinct possible infection sequences, modulo 10^9 + 7.
Example 1: n = 5, infected = [2]. Day 0: 2; Day 1: 1, 3; Day 2: 0, 4. Houses on the same day can appear in either order, giving [2,1,3,0,4], [2,1,3,4,0], [2,3,1,0,4], [2,3,1,4,0]. Answer: 4.
Example 2: n = 6, infected = [2, 4]. The infection spreads simultaneously from both sources; count all distinct valid sequences.
The code had to run and pass all test cases on HackerRank.
Also asked as · Count the distinct infection sequences across a line of houses.
Community answers
Related courses



