-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC2466.java
More file actions
60 lines (48 loc) · 1.58 KB
/
LC2466.java
File metadata and controls
60 lines (48 loc) · 1.58 KB
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
52
53
54
55
56
57
58
59
60
/*
* LC2466
*/
import java.util.*;
public class LC2466 {
public static int countGoodStrings(int low, int high, int zero, int one) {
int maxLen = high + Math.max(zero, one);
int[] dp = new int[maxLen + 1];
Arrays.fill(dp, -1);
return recur(low, high, zero, one, 0, dp);
}
public static int recur(int low, int high, int zero, int one, int len, int[] dp) {
// Base case
if (len > high) {
dp[len] = 0;
return 0;
}
if (dp[len] != -1) {
return dp[len];
}
int zeroLen = len + zero;
int oneLen = len + one;
int zeroCount = (zeroLen >= low && zeroLen <= high) ? 1 : 0;
int oneCount = (oneLen >= low && oneLen <= high) ? 1 : 0;
int res = zeroCount + recur(low, high, zero, one, zeroLen, dp) + oneCount
+ recur(low, high, zero, one, oneLen, dp);
dp[len] = res % (1000000007);
return dp[len];
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Low : ");
int low = sc.nextInt();
System.out.println();
System.out.print("Enter High : ");
int high = sc.nextInt();
System.out.println();
System.out.print("Enter Zero : ");
int zero = sc.nextInt();
System.out.println();
System.out.print("Enter One : ");
int one = sc.nextInt();
System.out.println();
int ans = countGoodStrings(low, high, zero, one);
System.out.println(ans);
sc.close();
}
}