Leetcode #2327: Number of People Aware of a Secret
In this guide, we solve Leetcode #2327 Number of People Aware of a Secret in Python and focus on the core idea that makes the solution efficient.
You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

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.
Quick Facts
- Difficulty: Medium
- Premium: No
- Tags: Queue, Dynamic Programming, Simulation
Intuition
The problem breaks into overlapping subproblems, so caching results prevents exponential repetition.
A carefully chosen DP state captures exactly what we need to build the final answer.
Approach
Define the DP state and recurrence, then compute states in the correct order.
Optionally compress space once the recurrence is clear.
Steps:
- Choose a DP state definition.
- Write the recurrence and base cases.
- Compute states in the correct order.
Example
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)
Python Solution
class Solution:
def peopleAwareOfSecret(self, n: int, delay: int, forget: int) -> int:
m = (n << 1) + 10
d = [0] * m
cnt = [0] * m
cnt[1] = 1
for i in range(1, n + 1):
if cnt[i]:
d[i] += cnt[i]
d[i + forget] -= cnt[i]
nxt = i + delay
while nxt < i + forget:
cnt[nxt] += cnt[i]
nxt += 1
mod = 10**9 + 7
return sum(d[: n + 1]) % mod
Complexity
The time complexity is , and the space complexity is , where is the given integer. The space complexity is , where is the given integer.
Edge Cases and Pitfalls
Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.
Summary
This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.