-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0552.StudentAttendanceRecordII.cpp
More file actions
62 lines (53 loc) · 1.42 KB
/
0552.StudentAttendanceRecordII.cpp
File metadata and controls
62 lines (53 loc) · 1.42 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
class Solution {
public:
static constexpr int modFactor = 1000000007;
static constexpr int maxAbsences = 2;
static constexpr int maxLates = 3;
int checkRecord(int n) {
// Speed thingies.
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
// Setup layers.
int
** previousLayer = new int* [maxAbsences],
** activeLayer = new int* [maxAbsences];
for (int i = 0; i < maxAbsences; i++) {
previousLayer[i] = new int[maxLates];
activeLayer[i] = new int[maxLates];
}
// Initialize starting layer.
for (int j = 0; j < maxAbsences; j++)
for (int k = 0; k < maxLates; k++)
previousLayer[j][k] = 1;
for (int i = 0; i <= n; i++) {
// Clear layer.
for (int j = 0; j < maxAbsences; j++)
for (int k = 0; k < maxLates; k++)
activeLayer[j][k] = 0;
// Process layer.
for (int j = 0; j < maxAbsences; j++) {
for (int k = 0; k < maxLates; k++) {
// Present.
activeLayer[j][k] += previousLayer[j][2];
// Late.
if (k > 0) {
activeLayer[j][k] += previousLayer[j][k - 1];
activeLayer[j][k] %= modFactor;
}
// Absent.
if (j > 0) {
activeLayer[j][k] += previousLayer[j - 1][2];
activeLayer[j][k] %= modFactor;
}
}
}
// Swap layers.
int** temp = activeLayer;
activeLayer = previousLayer;
previousLayer = temp;
}
// Return records with 1 absence and 2 lates or better.
return activeLayer[1][2];
}
};