-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
197 lines (174 loc) · 5.51 KB
/
main.cpp
File metadata and controls
197 lines (174 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Source: https://leetcode.com/problems/number-of-people-aware-of-a-secret
// Title: Number of People Aware of a Secret
// Difficulty: Medium
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 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** `10^9 + 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`
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <vector>
using namespace std;
// Use 2-D DP
//
// DP[i,j] is the number of people who have known the secret for exactly j days, at day i.
// DP[i,j] = DP[i-1,j-1], j > 0
// DP[i,0] = Sum DP[i-1,j-1] for j in [delay, forget)
class Solution {
constexpr static int modulo = 1e9 + 7;
public:
int peopleAwareOfSecret(int n, int delay, int forget) {
auto dp = vector(n + 1, vector(forget, 0));
dp[1][0] = 1;
for (auto i = 2; i <= n; ++i) {
for (auto j = 1; j < forget; ++j) {
dp[i][j] = dp[i - 1][j - 1];
}
for (auto j = delay; j < forget; ++j) {
dp[i][0] = (dp[i][0] + dp[i - 1][j - 1]) % modulo;
}
}
auto ans = 0;
for (auto j = 0; j < forget; ++j) {
ans = (ans + dp[n][j]) % modulo;
}
return ans;
}
};
// Use 1-D DP
//
// DP[i,j] is the number of people who have known the secret for exactly j days, at day i.
// DP[i,j] = DP[i-1,j-1], j > 0
// DP[i,0] = Sum DP[i-1,j-1] for j in [delay, forget)
class Solution2 {
constexpr static int modulo = 1e9 + 7;
public:
int peopleAwareOfSecret(int n, int delay, int forget) {
auto curr = vector(forget, 0);
auto prev = vector(forget, 0);
curr[0] = 1;
for (auto i = 2; i <= n; ++i) {
swap(curr, prev);
for (auto j = 1; j < forget; ++j) {
curr[j] = prev[j - 1];
}
curr[0] = 0;
for (auto j = delay; j < forget; ++j) {
curr[0] = (curr[0] + prev[j - 1]) % modulo;
}
}
auto ans = 0;
for (auto j = 0; j < forget; ++j) {
ans = (ans + curr[j]) % modulo;
}
return ans;
}
};
// Use 1-D DP
//
// Let DP2[i-j] = DP[i,j] to avoid update for j > 0
class Solution3 {
constexpr static int modulo = 1e9 + 7;
public:
int peopleAwareOfSecret(int n, int delay, int forget) {
auto dp = vector(forget, 0);
dp[1] = 1;
for (auto i = 2; i <= n; ++i) {
auto newbie = 0;
for (auto j = delay; j < forget; ++j) {
newbie = (newbie + dp[(i - j + forget) % forget]) % modulo;
}
dp[i % forget] = newbie;
}
auto ans = 0;
for (auto j = 0; j < forget; ++j) {
ans = (ans + dp[j]) % modulo;
}
return ans;
}
};
// Use 1-D DP
//
// Use as extra variable to track the number of people that will tell the secret.
// S[i] = S[i-1] + DP[i-1,delay-1] - DP[i-1,forget-1]
class Solution4 {
constexpr static int modulo = 1e9 + 7;
public:
int peopleAwareOfSecret(int n, int delay, int forget) {
auto curr = vector(forget, 0);
auto prev = vector(forget, 0);
curr[0] = 1;
auto snitches = 0;
for (auto i = 2; i <= n; ++i) {
swap(curr, prev);
for (auto j = 1; j < forget; ++j) {
curr[j] = prev[j - 1];
}
snitches = (snitches + prev[delay - 1]) % modulo;
snitches = (snitches - prev[forget - 1] + modulo) % modulo;
curr[0] = snitches;
}
auto ans = 0;
for (auto j = 0; j < forget; ++j) {
ans = (ans + curr[j]) % modulo;
}
return ans;
}
};
// Use 1-D DP
//
// Use DP2[i-j].
// Use extra variable.
class Solution5 {
constexpr static int modulo = 1e9 + 7;
public:
int peopleAwareOfSecret(int n, int delay, int forget) {
auto dp = vector(forget, 0);
dp[1] = 1;
auto snitches = 0;
for (auto i = 2; i <= n; ++i) {
snitches = (snitches + dp[(i - delay + forget) % forget]) % modulo;
snitches = (snitches - dp[i % forget] + modulo) % modulo;
dp[i % forget] = snitches;
}
auto ans = 0;
for (auto j = 0; j < forget; ++j) {
ans = (ans + dp[j]) % modulo;
}
return ans;
}
};