Skip to content

Commit 666db38

Browse files
authored
Create Q-01: Dice Combinations.java
1 parent 6e81093 commit 666db38

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

CSES-DP/Q-01: Dice Combinations.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
Author : Saurabh Kumar
3+
Problem Link :https://cses.fi/problemset/task/1633
4+
*/
5+
6+
import java.util.Arrays;
7+
import java.util.Scanner;
8+
9+
public class DiceCombinations {
10+
static long MOD = 1000000007 ;
11+
static long[] dp = new long[10000007];
12+
public static void main(String[] args) {
13+
Scanner sc = new Scanner(System.in);
14+
int n = sc.nextInt();
15+
Arrays.fill(dp, -1);
16+
System.out.println(solveTab(n));
17+
}
18+
// TLE ::)
19+
public static long solveRec(int n) {
20+
//Base Case ::
21+
if (n == 0)
22+
return 1;
23+
if (dp[n] != -1) {
24+
return dp[n];
25+
}
26+
long ans = 0;
27+
for (int i = 1; i <= Math.min(n, 6); i++) {
28+
ans = (ans + solveRec(n - i)) % MOD;
29+
}
30+
return dp[n] = ans;
31+
}
32+
33+
public static long solveTab(int n) {
34+
long[] dp = new long[n + 1];
35+
36+
dp[0] = 1;
37+
for (int i = 1; i <=n; i++) {
38+
for (int j = 0; j <= Math.min(i,6); j++) {
39+
dp[i] += dp[i - j];
40+
dp[i] %= MOD;
41+
}
42+
}
43+
return dp[n];
44+
}
45+
}

0 commit comments

Comments
 (0)