[Solved] Number of People Aware of a Secret LeetCode Contest Problem

On day 1, one person discovers a secret.

Number of People Aware of 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.

Number of People Aware of a Secret LeetCode Contest

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

Sliding window, calculating the number of people who will share the secrets.

Explanation

dp[i] means the number of people who found the secret on ith day.
share is the number of people who are going to share the secrets.
On the ith day,
dp[i - delay] people found the secret delay days before,
starting to share the secret.
so share += dp[i - delay]

dp[i - forget] people found the secret forget days before,
and forgot the secret today.
so share -= dp[i - forget]
and we assign dp[i] = share.

Complexity

Time O(n)
Space O(n)

Method 1

Java

    public int peopleAwareOfSecret(int n, int delay, int forget) {
        long dp[] = new long[n + 1], mod = (long)1e9 + 7, share = 0, res = 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;
        for (int i = n - forget + 1; i <= n; ++i)
            res = (res + dp[i]) % mod;
        return (int)res;
    }

C++

    int peopleAwareOfSecret(int n, int delay, int forget) {
        vector<long> dp(n + 1);
        dp[1] = 1;
        int share = 0, mod = 1e9 + 7, res = 0 ;
        for (int i = 2; i <= n; ++i)
            dp[i] = share = (share + dp[max(i - delay, 0)] - dp[max(i - forget, 0)] + mod) % mod;
        for (int i = n - forget + 1; i <= n; ++i)
            res = (res + dp[i]) % mod;
        return res;
    }

Python

    def peopleAwareOfSecret(self, n, delay, forget):
        dp = [1] + [0] * (n - 1)
        mod = 10 ** 9 + 7
        share = 0
        for i in range(1, n):
            dp[i] = share = (share + dp[i - delay] - dp[i - forget]) % mod
        return sum(dp[-forget:]) % mod

Method 2

Time O(n)
Space O(forget)

C++

    int peopleAwareOfSecret(int n, int delay, int forget) {
        vector<long> dp(forget);
        dp[0] = 1;
        long mod = 1e9 + 7, share = 0;
        for (int i = 1; i < n; ++i)
            dp[i % forget] = share = (share + dp[(i - delay + forget) % forget] - dp[i % forget] + mod) % mod;
        return accumulate(dp.begin(), dp.end(), 0L) % mod;
    }

Python

    def peopleAwareOfSecret(self, n, delay, forget):
        dp = [1] + [0] * forget
        mod = 10 ** 9 + 7
        share = 0
        for i in range(1, n):
            dp[i % forget] = share = (share + dp[(i - delay) % forget] - dp[i % forget]) % mod
        return sum(dp) % mod
Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *