-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathCountBoardPathDP.java
49 lines (38 loc) Β· 1.06 KB
/
CountBoardPathDP.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
package section19_DynamicProgramming;
public class CountBoardPathDP {
public static void main(String[] args) {
int n = 10;
System.out.println(countBoardPath(n, 0, new int[n + 1])); // 492
System.out.println(countBoardPathIterative(n)); // 492
}
// O(N) Time - recursive storage
public static int countBoardPath(int end, int current, int[] storage) {
if (current == end)
return 1;
if (current > end)
return 0;
if (storage[current] != 0)
return storage[current];
int count = 0;
for (int dice = 1; dice <= 6; dice++) {
count = count + countBoardPath(end, current + dice, storage);
}
storage[current] = count;
return count;
}
// iterative storage
// O(6N) ~ O(N) Time | O(N) Space
public static int countBoardPathIterative(int n) {
int[] storage = new int[n + 1];
// seed
storage[n] = 1;
for (int index = n - 1; index >= 0; index--) {
int currSum = 0;
for (int dice = index + 1; dice <= index + 6 && dice <= n; dice++) {
currSum = currSum + storage[dice];
}
storage[index] = currSum;
}
return storage[0];
}
}