Skip to content

Commit f966804

Browse files
authored
Create 1269. Number of Ways to Stay in the Same Place After Some Steps.cpp
1 parent 14ed0b4 commit f966804

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
int mod = pow(10, 9) + 7;
3+
vector<int> ways{0, -1, 1};
4+
vector<vector<int>> dp;
5+
public:
6+
int numWays(int n, int len) {
7+
dp.resize(n+1, vector<int>((n)/2 + 1, -1));
8+
return solve(n, len);
9+
}
10+
11+
int solve(int n, int len, int pos = 0) {
12+
if(n == 0 && pos == 0) return 1;
13+
14+
if(pos < 0 || pos >= len || n == 0 || pos > n) return 0;
15+
16+
if(dp[n][pos] != -1) return dp[n][pos];
17+
18+
int ans = 0;
19+
for(int i=0; i<3; i++) {
20+
ans = (ans % mod + solve(n-1, len, pos + ways[i]) % mod) % mod;
21+
}
22+
23+
return dp[n][pos] = ans % mod;
24+
25+
}
26+
};

0 commit comments

Comments
 (0)