generated from threeal/project-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolution.cpp
32 lines (29 loc) · 971 Bytes
/
solution.cpp
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
#include <utility>
#include <vector>
class Solution {
public:
int numRollsToTarget(int n, int k, int target) {
std::vector<int> prevCounts(target, 0), counts(target, 0);
std::fill(prevCounts.begin(), prevCounts.begin() + std::min(k, target), 1);
for (int i{1}; i < n; ++i) {
long long sum{0};
if (prevCounts.size() <= static_cast<std::size_t>(i + k)) {
for (std::size_t j = i; j < prevCounts.size(); ++j) {
sum = (sum + prevCounts[j - 1]) % 1000000007;
counts[j] = sum;
}
} else {
for (int j{i}; j < i + k; ++j) {
sum = (sum + prevCounts[j - 1]) % 1000000007;
counts[j] = sum;
}
for (std::size_t j = i + k; j < prevCounts.size(); ++j) {
sum = (1000000007 + sum - prevCounts[j - 1 - k] + prevCounts[j - 1]) % 1000000007;
counts[j] = sum;
}
}
std::swap(counts, prevCounts);
}
return prevCounts.back();
}
};