Skip to content

Latest commit

 

History

History
47 lines (35 loc) · 1.76 KB

NumberofPeopleAwareofaSecret.md

File metadata and controls

47 lines (35 loc) · 1.76 KB

The question

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)

Approach:-

Screenshot 2022-07-09 at 4 33 14 PM

Lets code:-

public int peopleAwareOfSecret(int n, int delay, int forget) {
    long dp[] = new long[n+1], mod = (long)1e9+7, share = 0, resp = 0;
    dp[1] = 1;
    for(int i=2; i<=n; i++){
        dp[i] = share = share + (dp[Math.max(i-delay, 0)] - dp[Math.max(i-forget, 0)] + mod)%mod;
    }

    //n - forget + 1 indicates that counting is done from the day first person shares the secret to another person
    for(int i=n - forget + 1; i<=n; i++){
        resp = (resp+dp[i])%mod;
    }

    return (int)resp;
}

Time Complexity - O(n) Space Complexity - O(n)