leetcode
leetcode 2501 ~ 2550
统计感冒序列的数目

统计感冒序列的数目

难度:

标签:

题目描述

You are given an integer n and a 0-indexed integer array sick which is sorted in increasing order.

There are n children standing in a queue with positions 0 to n - 1 assigned to them. The array sick contains the positions of the children who are infected with an infectious disease. An infected child at position i can spread the disease to either of its immediate neighboring children at positions i - 1 and i + 1 if they exist and are currently not infected. At most one child who was previously not infected can get infected with the disease in one second.

It can be shown that after a finite number of seconds, all the children in the queue will get infected with the disease. An infection sequence is the sequential order of positions in which all of the non-infected children get infected with the disease. Return the total number of possible infection sequences.

Since the answer may be large, return it modulo 109 + 7.

Note that an infection sequence does not contain positions of children who were already infected with the disease in the beginning.

 

Example 1:

Input: n = 5, sick = [0,4]
Output: 4
Explanation: Children at positions 1, 2, and 3 are not infected in the beginning. There are 4 possible infection sequences:
- The children at positions 1 and 3 can get infected since their positions are adjacent to the infected children 0 and 4. The child at position 1 gets infected first.
Now, the child at position 2 is adjacent to the child at position 1 who is infected and the child at position 3 is adjacent to the child at position 4 who is infected, hence either of them can get infected. The child at position 2 gets infected.
Finally, the child at position 3 gets infected because it is adjacent to children at positions 2 and 4 who are infected. The infection sequence is [1,2,3].
- The children at positions 1 and 3 can get infected because their positions are adjacent to the infected children 0 and 4. The child at position 1 gets infected first.
Now, the child at position 2 is adjacent to the child at position 1 who is infected and the child at position 3 is adjacent to the child at position 4 who is infected, hence either of them can get infected. The child at position 3 gets infected.
Finally, the child at position 2 gets infected because it is adjacent to children at positions 1 and 3 who are infected. The infection sequence is [1,3,2].
- The infection sequence is [3,1,2]. The order of infection of disease in the children can be seen as: [0,1,2,3,4] => [0,1,2,3,4] => [0,1,2,3,4] => [0,1,2,3,4].
- The infection sequence is [3,2,1]. The order of infection of disease in the children can be seen as: [0,1,2,3,4] => [0,1,2,3,4] => [0,1,2,3,4] => [0,1,2,3,4].

Example 2:

Input: n = 4, sick = [1]
Output: 3
Explanation: Children at positions 0, 2, and 3 are not infected in the beginning. There are 3 possible infection sequences:
- The infection sequence is [0,2,3]. The order of infection of disease in the children can be seen as: [0,1,2,3] => [0,1,2,3] => [0,1,2,3] => [0,1,2,3].
- The infection sequence is [2,0,3]. The order of infection of disease in the children can be seen as: [0,1,2,3] => [0,1,2,3] => [0,1,2,3] => [0,1,2,3].
- The infection sequence is [2,3,0]. The order of infection of disease in the children can be seen as: [0,1,2,3] => [0,1,2,3] => [0,1,2,3] => [0,1,2,3].

 

Constraints:

  • 2 <= n <= 105
  • 1 <= sick.length <= n - 1
  • 0 <= sick[i] <= n - 1
  • sick is sorted in increasing order.

代码结果

运行时间: 64 ms, 内存: 20.6 MB


/*
 * Problem Statement:
 * Given an integer n and a 0-indexed integer array sick, sorted in ascending order,
 * find the number of different sequences in which all initially non-sick children will get infected.
 * The infection spreads to the adjacent left or right child who is not yet infected.
 * Return the number of different infection sequences modulo 10^9 + 7.
 */

import java.util.stream.IntStream;

public class Solution {
    private static final int MOD = 1000000007;

    public int countInfectionSequences(int n, int[] sick) {
        int healthyCount = n - sick.length;
        return IntStream.rangeClosed(1, healthyCount)
                .reduce(1, (a, b) -> (int) ((long) a * b % MOD));
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.countInfectionSequences(5, new int[]{0, 4})); // Output: 4
        System.out.println(solution.countInfectionSequences(4, new int[]{1})); // Output: 3
    }
}

解释

方法:

题解的主要思路是将问题转化为排列组合问题。给定n位小朋友中,一些已经感冒的小朋友分割出不同的段,每个段内部的小朋友是独立的,并可以在各自的段内自由感染。首先,计算整体可能的感冒序列数目为感冒小朋友之间未感冒小朋友的全排列,乘以边界未感冒小朋友的排列方式。为了计算排列方式,预先计算出阶乘数和使用费马小定理进行模逆计算,因为直接的除法运算在模运算中不适用。另外,使用快速幂计算2的幂,因为每一对小朋友之间的间隔都存在两种传染可能性。

时间复杂度:

O(n)

空间复杂度:

O(n)

代码细节讲解

🦆
如何处理`sick`数组中相邻两个元素相等的情况,即两个病患小朋友紧挨着,这种情况下如何计算序列数目?
在`sick`数组中如果存在相邻两个元素相等的情况,即两个已感冒的小朋友紧挨着,这表示在他们之间没有未感冒的小朋友。在这种情况下,tmp(两个sick小朋友之间的未感冒小朋友数量)为0,因此在计算各部分的阶乘时,tmp对应的阶乘是factorial[0],即1。此外,因为没有未感冒的小朋友,所以也不会有新增的传染方式(2的幂次项不增加)。因此,这种情况不会对病毒传播的序列数目产生影响,可以简单地将这两个相等的sick索引视为一个,计算时跳过其中一个即可。
🦆
题解中提到每一秒中至多一位还没感冒的小朋友会被传染,那么在处理多个未感冒小朋友的区间时,感染的顺序是否会影响最终的序列数目?
在给定的模型中,虽然每秒中至多一位未感冒的小朋友会被传染,但是整体的传染序列数目是由未感冒小朋友的全排列决定的。因为每个未感冒的小朋友最终都将被感染,其感染的具体顺序不影响最终的序列数目。我们计算的是所有可能的排列方式,这包括了所有可能的感染顺序。因此,虽然每秒的感染事件是序列化的,整体来看,感染顺序的不同配置不会影响最终可能的序列总数。这里的关键在于理解,尽管过程是序列化,所有可能的排列组合都被考虑在内。
🦆
题解说明了使用费马小定理进行模逆计算,但没有详细说明这一步的具体实现和原理,能否详细解释一下费马小定理在这里的应用方法?
费马小定理指出,如果p是一个质数,那么对于任何整数a,只要a不是p的倍数,就有a的(p-1)次方与1同余,即a^(p-1) ≡ 1 (mod p)。从这一定理可以推导出,a的(p-2)次方就是a模p的乘法逆元,即a^(p-2) ≡ a^(-1) (mod p)。在题解中,因为模数mod是一个质数(10^9+7),我们需要对某些运算结果取逆元以进行除法运算,这时就可以利用费马小定理。具体地,如果要计算v的逆元,我们计算v^(mod-2) % mod。这在Python中可以通过pow函数实现,即pow(v, mod-2, mod),这样就能得到v在模mod下的逆,以进行之后的除法运算。

相关问题