返回介绍

solution / 2300-2399 / 2327.Number of People Aware of a Secret / README_EN

发布于 2024-06-17 01:03:07 字数 5509 浏览 0 评论 0 收藏 0

2327. Number of People Aware of a Secret

中文文档

Description

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)

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

Solutions

Solution 1

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
class Solution {
  private static final int MOD = (int) 1e9 + 7;

  public int peopleAwareOfSecret(int n, int delay, int forget) {
    int m = (n << 1) + 10;
    long[] d = new long[m];
    long[] cnt = new long[m];
    cnt[1] = 1;
    for (int i = 1; i <= n; ++i) {
      if (cnt[i] > 0) {
        d[i] = (d[i] + cnt[i]) % MOD;
        d[i + forget] = (d[i + forget] - cnt[i] + MOD) % MOD;
        int nxt = i + delay;
        while (nxt < i + forget) {
          cnt[nxt] = (cnt[nxt] + cnt[i]) % MOD;
          ++nxt;
        }
      }
    }
    long ans = 0;
    for (int i = 1; i <= n; ++i) {
      ans = (ans + d[i]) % MOD;
    }
    return (int) ans;
  }
}
using ll = long long;
const int mod = 1e9 + 7;

class Solution {
public:
  int peopleAwareOfSecret(int n, int delay, int forget) {
    int m = (n << 1) + 10;
    vector<ll> d(m);
    vector<ll> cnt(m);
    cnt[1] = 1;
    for (int i = 1; i <= n; ++i) {
      if (!cnt[i]) continue;
      d[i] = (d[i] + cnt[i]) % mod;
      d[i + forget] = (d[i + forget] - cnt[i] + mod) % mod;
      int nxt = i + delay;
      while (nxt < i + forget) {
        cnt[nxt] = (cnt[nxt] + cnt[i]) % mod;
        ++nxt;
      }
    }
    int ans = 0;
    for (int i = 1; i <= n; ++i) ans = (ans + d[i]) % mod;
    return ans;
  }
};
func peopleAwareOfSecret(n int, delay int, forget int) int {
  m := (n << 1) + 10
  d := make([]int, m)
  cnt := make([]int, m)
  mod := int(1e9) + 7
  cnt[1] = 1
  for i := 1; i <= n; i++ {
    if cnt[i] == 0 {
      continue
    }
    d[i] = (d[i] + cnt[i]) % mod
    d[i+forget] = (d[i+forget] - cnt[i] + mod) % mod
    nxt := i + delay
    for nxt < i+forget {
      cnt[nxt] = (cnt[nxt] + cnt[i]) % mod
      nxt++
    }
  }
  ans := 0
  for i := 1; i <= n; i++ {
    ans = (ans + d[i]) % mod
  }
  return ans
}
function peopleAwareOfSecret(n: number, delay: number, forget: number): number {
  let dp = new Array(n + 1).fill(0n);
  dp[1] = 1n;
  for (let i = 2; i <= n; i++) {
    let pre = 0n;
    for (let j = i - forget + 1; j <= i - delay; j++) {
      if (j > 0) {
        pre += dp[j];
      }
    }
    dp[i] = pre;
  }
  let pre = 0n;
  let i = n + 1;
  for (let j = i - forget; j < i; j++) {
    if (j > 0) {
      pre += dp[j];
    }
  }
  return Number(pre % BigInt(10 ** 9 + 7));
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文