[java] 2327. Number of People Aware of a Secret

Viewer

copydownloadembedprintName: 2327. Number of People Aware of a Secret
  1. /*
  2. On day 1, one person discovers a secret.
  3.  
  4. 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.
  5.  
  6. 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.
  7.  
  8. Example 1:
  9.  
  10. Input: n = 6, delay = 2, forget = 4
  11. Output: 5
  12. Explanation:
  13. Day 1: Suppose the first person is named A. (1 person)
  14. Day 2: A is the only person who knows the secret. (1 person)
  15. Day 3: A shares the secret with a new person, B. (2 people)
  16. Day 4: A shares the secret with a new person, C. (3 people)
  17. Day 5: A forgets the secret, and B shares the secret with a new person, D. (3 people)
  18. Day 6: B shares the secret with E, and C shares the secret with F. (5 people)
  19.  
  20. */
  21.  
  22.  
  23. class Solution {
  24.     public int peopleAwareOfSecret(int n, int delay, int forget) {
  25.         int count = 1;
  26.         int canTell = 0;
  27.         int toForget[];
  28.         int toDelay[];
  29.         toForget = new int[n+forget];
  30.         toDelay = new int[n+delay];
  31.         
  32.         //do day 1 for one person
  33.         toForget[forget] = count;
  34.         toDelay[delay] = count;
  35.         
  36.         //cycle through days
  37.         for(int i=1; i<n; i++){            
  38.             //how many can tell
  39.             canTell += toDelay[i];
  40.             canTell -= toForget[i];
  41.             
  42.             //tell secret and upkeep newbies
  43.             int newbies = canTell;
  44.             toForget[i+forget] = newbies;
  45.             toDelay[i+delay] = newbies;
  46.             
  47.             
  48.             //upkeep total count
  49.             count += newbies;
  50.             count -= toForget[i];
  51.         }
  52.         
  53.         return count;
  54.     }
  55. }

Editor

You can edit this paste and save as new:


File Description
  • 2327. Number of People Aware of a Secret
  • Paste Code
  • 28 Sep-2022
  • 1.96 Kb
You can Share it: