-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode1269.cpp
More file actions
27 lines (26 loc) · 857 Bytes
/
LeetCode1269.cpp
File metadata and controls
27 lines (26 loc) · 857 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
25
26
27
class Solution
{
int mod = pow(10, 9) + 7;
vector<vector<int>> dp;
public:
int solve(int pos, int steps, int len)
{
if (steps == 0 and pos == 0)
return 1;
else if (pos < 0 or pos == len or steps == 0 or pos > steps)
return 0;
else if (dp[steps][pos] != -1)
return dp[steps][pos];
int ans = 0;
ans = (ans % mod + solve(pos, steps - 1, len) % mod) % mod; // stay
ans = (ans % mod + solve(pos + 1, steps - 1, len) % mod) % mod; // move right
ans = (ans % mod + solve(pos - 1, steps - 1, len) % mod) % mod; // move left
dp[steps][pos] = ans % mod;
return dp[steps][pos];
}
int numWays(int steps, int arrLen)
{
dp.resize(steps + 1, vector<int>(steps / 2 + 1, -1));
return solve(0, steps, arrLen);
}
};