-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1269.cpp
More file actions
24 lines (23 loc) · 732 Bytes
/
1269.cpp
File metadata and controls
24 lines (23 loc) · 732 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
const int MODULO = 1000000007;
int numWays(int steps, int arrLen) {
int maxColumn = min(arrLen - 1, steps);
vector<int> dp(maxColumn + 1);
dp[0] = 1;
for (int i = 1; i <= steps; i++) {
vector<int> dpNext(maxColumn + 1);
for (int j = 0; j <= maxColumn; j++) {
dpNext[j] = dp[j];
if (j - 1 >= 0) {
dpNext[j] = (dpNext[j] + dp[j - 1]) % MODULO;
}
if (j + 1 <= maxColumn) {
dpNext[j] = (dpNext[j] + dp[j + 1]) % MODULO;
}
}
dp = dpNext;
}
return dp[0];
}
};