Number of People Aware of a Secret LeetCode solution in Java

1
class Solution {
2
public int peopleAwareOfSecret(int n, int delay, int forget) {
3
long mod = 1000000007L;
4
long[] shares = new long[n + 1];
5
long[] forgets = new long[n + 1];
6

7
if (delay < n) {
8
shares[delay + 1] = 1;
9
}
10
if (forget < n) {
11
forgets[forget + 1] = 1;
12
}
13

14
long shareToday = 0;
15
long peopleKnow = 1;
16
for (int i = delay; i <= n; i++) {
17
shareToday += shares[i] % mod;
18
shareToday -= forgets[i] % mod;
19

20
peopleKnow -= forgets[i] % mod;
21
peopleKnow += shareToday % mod;
22

23
if (i + delay < n + 1) {
24
shares[i + delay] += shareToday % mod;
25
}
26
if (i + forget < n + 1) {
27
forgets[i + forget] += shareToday % mod;
28
}
29
}
30

31
return (int) (peopleKnow % mod);
32
}
33
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0