forked from akkupy/codeDump
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCountWaysToBuildGoodString.cpp
41 lines (30 loc) · 1.1 KB
/
CountWaysToBuildGoodString.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
33
34
35
36
37
38
39
40
41
// https://leetcode.com/problems/count-ways-to-build-good-strings/description/
class Solution {
public:
int solve(int len, int low, int high, int zero, int one){
if(len>high)
return 0;
int ans= 0;
ans= solve(len+zero, low, high, zero, one)%1000000007;
ans= (ans + solve(len+one, low, high, zero, one)%1000000007)%1000000007;
return ans;
}
int solveMem(int len, int low, int high, int zero, int one, vector<int> &dp){
if(len>high)
return 0;
if(dp[len]!=-1)
return dp[len];
int ans= 0;
if(len>=low && len<=high)
ans++;
ans+= solveMem(len+zero, low, high, zero, one, dp)%1000000007;
ans= (ans%1000000007 + solveMem(len+one, low, high, zero, one, dp)%1000000007)%1000000007;
dp[len]= ans;
return dp[len];
}
int countGoodStrings(int low, int high, int zero, int one) {
// return solve(0, low, high, zero, one)%1000000007;
vector<int> dp(high+1, -1);
return solveMem(0, low, high, zero, one, dp);
}
};