当前位置:网站首页>2022.07.03 (LC 6109 number of people who know secrets)

2022.07.03 (LC 6109 number of people who know secrets)

2022-07-05 00:14:00 Leeli9316

  Method : Dynamic programming

class Solution {
    public int peopleAwareOfSecret(int n, int delay, int forget) {
        int MOD = 1000000007;
        // state :f[i] It means the first one i Tianxin knows the number of Secrets 
        int[] f = new int[n + 1];
        f[1] = 1;
        for (int i = 2; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                // In the i God , Only (i - forget, i - delay] New people who know the secret in this range will tell others 
                // Be careful i - forget + 1 and  i - delay Maybe more than [1, n] The scope of the , We need to deal with the boundary 
                if (j > i - forget && j <= i - delay) {
                    f[i] = (f[i] + f[j]) % MOD;
                }
            }
        } 
        int ans = 0;
        for (int i = 1; i <= n; i++) {
            // The answer is in No n Heaven has not forgotten the sum of the secret numbers 
            if (i + forget > n) {
                ans = (ans + f[i]) % MOD;
            }
        }
        return ans;
    }
}

原网站

版权声明
本文为[Leeli9316]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/186/202207050008151736.html