-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathcount-routes-to-climb-a-rectangular-grid.cpp
More file actions
36 lines (33 loc) · 1.22 KB
/
count-routes-to-climb-a-rectangular-grid.cpp
File metadata and controls
36 lines (33 loc) · 1.22 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
// Time: O(n * m)
// Space: O(m)
// dp, two pointers
class Solution {
public:
int numberOfRoutes(vector<string>& grid, int d) {
static const int MOD = 1e9 + 7;
const auto& update = [](const auto& dp, int d, const auto& arr) {
vector<int> new_dp(size(arr));
int curr = accumulate(cbegin(dp), cbegin(dp) + min(d, static_cast<int>(size(dp))), 0, [](const auto& accu, const auto& x) {
return (accu + x) % MOD;
});
for (int i = 0; i < size(arr); ++i) {
if (i - d - 1 >= 0) {
curr = ((curr - dp[i - d - 1]) % MOD + MOD) % MOD;
}
if (i + d < size(dp)) {
curr = (curr + dp[i + d]) % MOD;
}
new_dp[i] = arr[i] == '.' ? curr : 0;
}
return new_dp;
};
vector<int> dp(size(grid[0]), 1);
for (int i = size(grid) - 1; i >= 0; --i) {
dp = update(dp, i != size(grid) - 1 ? d - 1 : 0, grid[i]);
dp = update(dp, d, grid[i]);
}
return accumulate(cbegin(dp), cend(dp), 0, [](const auto& accu, const auto& x) {
return (accu + x) % MOD;
});
}
};