Leetcode Problem 2327. Number of People Aware of a Secret

2327. Number of People Aware of a Secret

Leetcode Solutions

Dynamic Programming with Sliding Window

  1. Initialize an array dp of length n to store the number of people who learn the secret on each day.
  2. Set dp[0] to 1 since one person knows the secret on day 1.
  3. Initialize a variable share to keep track of the number of people who can share the secret on a given day.
  4. Iterate over the days from 2 to n: a. Add the number of people who learned the secret delay days ago to share. b. Subtract the number of people who learned the secret forget days ago from share. c. Set dp[i] to the current value of share.
  5. Sum up the values in dp from n - forget + 1 to n to get the total number of people who still remember the secret at the end of day n.
  6. Return the sum modulo 10^9 + 7.
UML Thumbnail

Recursive Top-Down Dynamic Programming with Memoization

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...