当前位置:网站首页>2327. Number of people who know secrets (recursive)

2327. Number of people who know secrets (recursive)

2022-07-06 04:15:00 Harris-H

2327. Number of people who know the secret ( Recurrence )

A one-dimensional dp Can qwq. Make f i f_i fi It means the first one i i i Tianxin knows the number of Secrets .

The first i i i Days can be controlled by ( i − f o r g e t , i − d e l a y ] (i-forget,i-delay] (iforget,idelay] Recurrence .

Obviously, prefixes and , Achieve O ( n ) O(n) O(n)

The final answer is : ∑ i = n − f o r g e t + 1 n f i \sum\limits_{i=n-forget+1}^n f_i i=nforget+1nfi

class Solution {
    
    const int MOD = 1000000007;

public:
    int peopleAwareOfSecret(int n, int delay, int forget) {
    
        vector<long long> f(n + 1), g(n + 1);
        f[1] = g[1] = 1;
        for (int i = 2; i <= n; i++) {
    
            int L = max(0, i - forget);
            int R = max(0, i - delay);
            f[i] = (g[R] - g[L] + MOD) % MOD;
            g[i] = (g[i - 1] + f[i]) % MOD;
        }
        return (g[n] - g[max(0, n - forget)] + MOD) % MOD;
    }
};
原网站

版权声明
本文为[Harris-H]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060411332040.html