Taro Logo

Number of People Aware of a Secret

#5 Most AskedMedium
0 views
Topics:
ArraysDynamic Programming

On day 1, one person discovers a secret.

You are given an integer delay, which means that each person will share the secret with a new person every day, starting from delay days after discovering the secret. You are also given an integer forget, which means that each person will forget the secret forget days after discovering it. A person cannot share the secret on the same day they forgot it, or on any day afterwards.

Given an integer n, return the number of people who know the secret at the end of day n. Since the answer may be very large, return it modulo 109 + 7.

Example 1:

Input: n = 6, delay = 2, forget = 4
Output: 5
Explanation:
Day 1: Suppose the first person is named A. (1 person)
Day 2: A is the only person who knows the secret. (1 person)
Day 3: A shares the secret with a new person, B. (2 people)
Day 4: A shares the secret with a new person, C. (3 people)
Day 5: A forgets the secret, and B shares the secret with a new person, D. (3 people)
Day 6: B shares the secret with E, and C shares the secret with F. (5 people)

Example 2:

Input: n = 4, delay = 1, forget = 3
Output: 6
Explanation:
Day 1: The first person is named A. (1 person)
Day 2: A shares the secret with B. (2 people)
Day 3: A and B share the secret with 2 new people, C and D. (4 people)
Day 4: A forgets the secret. B, C, and D share the secret with 3 new people. (6 people)

Constraints:

  • 2 <= n <= 1000
  • 1 <= delay < forget <= n

Solution


Clarifying Questions

When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:

  1. What are the constraints on the values of $n$, $delay$, and $forget$, and can $n$ be very large?
  2. Since the number of people who know the secret can grow exponentially, should we return the final answer modulo $10^9 + 7$ to prevent integer overflow?
  3. On day $1$, is it given that exactly one person knows the secret?
  4. If a person forgets the secret on day $d$, do they still know the secret and are they able to share it on day $d$, or do they lose it at the start of day $d$?
  5. If $delay$ is greater than or equal to $forget$, does that mean nobody will ever share the secret, resulting in an expected output of $1$?

Brute Force Solution

Approach

This problem simulates the spread of a secret across a given number of days. By recording the number of people who discover the secret on each day, we can maintain a sliding window of actively sharing individuals. At the end of day n, we sum only the counts of people whose discovery dates fall within the forget window.

Here's how the algorithm would work step-by-step:

  1. Initialize an array to store the number of people who first learn the secret on each day up to day n.
  2. Set the initial person on day 1 and maintain a running counter of active sharers.
  3. Iterate from day 2 through day n, adding new people who reach the sharing delay threshold to the active pool.
  4. Remove people from the active sharer count on the day they forget the secret.
  5. Set the number of new people learning the secret on the current day equal to the active sharer count.
  6. Sum the counts of people who learned the secret between day n - forget + 1 and day n, returning the total modulo 10^9 + 7.

Code Implementation

def people_aware_of_secret(n: int, delay: int, forget: int) -> int:
    modulo = 10**9 + 7
    # Track the number of people who discover the secret on each day.
    new_sharers = [0] * (n + 1)
    new_sharers[1] = 1
    active_sharers = 0
    # Iterate through days to simulate sharing and forgetting.
    for current_day in range(2, n + 1):
        if current_day - delay >= 1:
            active_sharers = (active_sharers + new_sharers[current_day - delay]) % modulo
        if current_day - forget >= 1:
            active_sharers = (active_sharers - new_sharers[current_day - forget] + modulo) % modulo
        new_sharers[current_day] = active_sharers
    # Sum the people who still remember the secret by day n.
    total_knowing = 0
    for day_index in range(max(1, n - forget + 1), n + 1):
        total_knowing = (total_knowing + new_sharers[day_index]) % modulo
    return total_knowing

Big(O) Analysis

Time Complexity
O(n) – We iterate through the days from 2 to n once, updating running totals in constant time for each day, followed by a loop that iterates at most forget times.
Space Complexity
O(n) – We allocate an array of size n + 1 to store the count of people who discovered the secret on each day.

Optimal Solution

Approach

This problem simulates the spread and forgetting of a secret across several days. We maintain an array of the count of people who discover the secret on each day. A running count of people actively sharing the secret updates dynamically as older learners enter their sharing window and eventually forget.

Here's how the algorithm would work step-by-step:

  1. Initialize an array to store the number of people who learn the secret on each day up to day n.
  2. Set the first day to have one person discovering the secret, with all other days initialized to zero.
  3. Iterate from day 2 to day n while maintaining a running total of active sharers by adding those reaching the delay threshold and subtracting those who forget.
  4. Assign the count of newly informed people on the current day to the number of active sharers.
  5. Sum the number of people who learned the secret within the last forget days before day n concludes.
  6. Return the resulting sum modulo 10^9 + 7.

Code Implementation

def people_aware_of_secret(n: int, delay: int, forget: int) -> int:
    modulo = 10**9 + 7
    # Array tracking how many people discover the secret on each day
    new_knowers = [0] * (n + 1)
    new_knowers[1] = 1
    active_sharers = 0
    
    for current_day in range(2, n + 1):
        # Add people who can start sharing today and remove those who forget today
        can_share_day = current_day - delay
        forgot_day = current_day - forget
        active_sharers = (active_sharers + new_knowers[can_share_day] - new_knowers[forgot_day]) % modulo
        # Newly informed people today equals the number of active sharers
        new_knowers[current_day] = active_sharers
        
    # Sum all people who learned the secret and have not yet forgotten it by day n
    total_aware = sum(new_knowers[max(1, n - forget + 1):n + 1]) % modulo
    return total_aware

Big(O) Analysis

Time Complexity
O(n) – We loop from day 2 to day n once, performing constant-time arithmetic operations at each step, followed by a slice sum of at most forget elements.
Space Complexity
O(n) – An array of size n + 1 is used to keep track of the number of people who discover the secret on each day.

Edge Cases

n is equal to 1
How to Handle:
The algorithm should return 1 immediately since the single person who starts with the secret has not yet forgotten it.
delay is greater than n
How to Handle:
The loop will finish with only the original person knowing the secret, correctly returning 1.
forget is less than or equal to delay
How to Handle:
People will forget the secret before they can ever share it, so the result will always be 0 after day 1.
Large n values up to the problem constraints
How to Handle:
We use a dynamic programming approach with a rolling window or prefix sum to achieve O(n * forget) or O(n) time complexity, preventing time limit exceeded errors.
Integer overflow due to large result sizes on day n
How to Handle:
We must take the modulo of 10^9 + 7 at each day's calculation step to keep numbers within standard integer limits.
delay equals 1
How to Handle:
People start sharing the secret on the very next day after learning it, which the daily transition logic handles correctly.
0/12 completed