Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Count the Number of Infection Sequences

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "2954" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

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 109+7. Example 1: Input: n = 5, sick = [0,4] Output: 4 Explanation: There is a total of 6 different sequences overall. Valid infection sequences are [1,2,3], [1,3,2], [3,2,1] and [3,1,2]. [2,3,1] and [2,1,3] are not valid infection sequences because the person at index 2 cannot be infected at the first step. Example 2: Input: n = 4, sick = [1] Output: 3 Explanation: There is a total of 6 different sequences overall. Valid infection sequences are [0,2,3], [2,0,3] and [2,3,0]. [3,2,0], [3,0,2], and [0,3,2] are not valid infection sequences because the infection starts at the person at index 1, then the order of infection is 2, then 3, and hence 3 cannot be infected earlier than 2. Constraints: 2 <= n <= 105 1 <= sick.length <= n - 1 0 <= sick[i] <= n - 1 sick is sorted in increasing order.

Explanation

Here's the approach and the Python code:

  • Decomposition: Divide the problem into independent intervals between infected people. Within each interval, calculate the number of ways to infect the people to the left and right of the infected endpoints independently.
  • Combinations & Permutations: Use combinations to choose which people get infected from the left and which from the right. Then, use permutations to calculate the number of ways to order the infection sequence within each side.
  • Modular Arithmetic: Perform all calculations modulo 109 + 7 to prevent overflow.

  • Runtime Complexity: O(n), Storage Complexity: O(n)

Code

    def solve():
    n, sick = int(input().split()[0]), list(map(int, input().split()))

    MOD = 10**9 + 7

    def factorial(num):
        res = 1
        for i in range(1, num + 1):
            res = (res * i) % MOD
        return res

    def inverse(num):
        return pow(num, MOD - 2, MOD)

    def combinations(n, k):
        if k < 0 or k > n:
            return 0
        num = factorial(n)
        den = (factorial(k) * factorial(n - k)) % MOD
        return (num * inverse(den)) % MOD

    ans = 1
    total_uninfected = n - len(sick)

    # Handle the left side of the first infected person
    leftmost_sick = sick[0]
    ans = (ans * pow(2, leftmost_sick, MOD)) % MOD
    total_uninfected -= leftmost_sick

    # Handle intervals between infected people
    for i in range(len(sick) - 1):
        left_sick = sick[i]
        right_sick = sick[i + 1]
        interval_len = right_sick - left_sick - 1

        left_side = interval_len // 2
        right_side = interval_len - left_side

        ans = (ans * combinations(interval_len, left_side)) % MOD
        ans = (ans * factorial(left_side)) % MOD
        ans = (ans * factorial(right_side)) % MOD

        total_uninfected -= interval_len

    # Handle the right side of the last infected person
    rightmost_sick = sick[-1]
    ans = (ans * pow(2, n - 1 - rightmost_sick, MOD)) % MOD

    print(ans % MOD)

solve()

More from this blog

C

Chatmagic blog

2894 posts

Solving Leetcode Interviews in Seconds with AI: Count the Number of Infection Sequences