Advertisement

Number of People Aware of a Secret - LeetCode 2327 Solution

Number of People Aware of a Secret - Complete Solution Guide

Number of People Aware of a Secret is LeetCode problem 2327, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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 t

Detailed Explanation

The problem describes a scenario where one person initially knows a secret. Each person who knows the secret shares it with a new person every day starting after a 'delay' period. However, each person forgets the secret after a 'forget' period. The goal is to determine the total number of people who know the secret on day 'n', modulo 10^9 + 7.

Solution Approach

The solution uses dynamic programming to simulate the spread of the secret day by day. A DP array `dp` is maintained, where `dp[i]` represents the number of people who *learn* the secret on day `i`. We also track `sharing_count`, which is the number of people who are currently sharing the secret on a given day. This variable is updated each day by adding the number of people who started sharing the secret and subtracting the number of people who forgot the secret. Finally, the solution sums up the number of people who learned the secret in the last 'forget' days to find the total number of people who know the secret on day 'n'.

Step-by-Step Algorithm

  1. Step 1: Initialize a DP array `dp` of size `n+1` with all values set to 0. Set `dp[1] = 1` because one person knows the secret initially.
  2. Step 2: Initialize `sharing_count` to 0. This variable tracks the number of people currently sharing the secret.
  3. Step 3: Iterate from day 2 to day `n` (inclusive):
  4. Step 4: Update `sharing_count` by adding `dp[i - delay]` (number of people who started sharing on day `i - delay`) and subtracting `dp[i - forget]` (number of people who forgot the secret on day `i - forget`). Remember to use modulo arithmetic after each operation.
  5. Step 5: Set `dp[i] = sharing_count`. This stores the number of people who *learn* the secret on day `i`.
  6. Step 6: After the loop finishes, iterate from day `n - forget + 1` to day `n` (inclusive). Sum up the values of `dp[i]` for these days. These are the people who learned the secret within the last `forget` days and haven't forgotten it yet.
  7. Step 7: Return the total sum modulo 10^9 + 7.

Key Insights

  • Insight 1: Dynamic Programming: The number of people who know the secret on a given day depends on the number of people who shared the secret in the past and haven't yet forgotten it. This overlapping subproblems property strongly suggests DP.
  • Insight 2: Sharing Count: It's crucial to keep track of the number of people actively sharing the secret each day, as only these people contribute to the increase in the number of people who know the secret.
  • Insight 3: Modulo Arithmetic: Because the number of people can become very large, performing all calculations modulo 10^9 + 7 is necessary to prevent integer overflow and ensure correct results.
  • Insight 4: Focusing on new sharers: The core idea is to compute the number of *new* people learning the secret each day, rather than tracking *everyone* who knows the secret at any given day, which would be more complex.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Dynamic Programming, Queue, Simulation.

Companies

Asked at: Arcesium, NCR.