-
Notifications
You must be signed in to change notification settings - Fork 0
/
BuildGoodString_2466.java
51 lines (46 loc) · 1.43 KB
/
BuildGoodString_2466.java
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
42
43
44
45
46
47
48
49
50
51
package org.example;
import java.util.HashMap;
public class BuildGoodString_2466 {
static class Solution {
public int countGoodStrings(int low, int high, int zero, int one) {
int[] memo = new int[high+1];
memo[0] = 1;
int max = Math.max(zero, one);
int min = Math.min(zero, one);
memo[min] = 1;
if (max%min==0){
memo[max] = 2;
}
else {
memo[max] = 1;
}
for (int i=min;i<=high;i++){
if (i-max<0){
memo[i] = memo[i-min];
}
else {
memo[i] = (memo[i-min]+memo[i-max])%1000000007;
}
}
int sum = 0;
for (int i=low;i<=high;i++){
sum=(sum+memo[i])%1000000007;
}
return sum;
}
public int dp(HashMap<Integer, Integer> memo,int i, int low, int high){
if (memo.get(i) != null){
return memo.get(i);
}
if (i<low){
return 0;
}
memo.put(i, (dp(memo, i-low, low, high)+dp(memo, i-high, low, high))%1000000007);
return memo.get(i);
}
public static void main(String[] args) {
Solution s = new Solution();
System.out.println(s.countGoodStrings(3, 3, 1, 1));
}
}
}