File tree Expand file tree Collapse file tree 2 files changed +7
-4
lines changed
slides/dynamicprogramming
src/main/java/com/williamfiset/algorithms/dp/examples/scenes Expand file tree Collapse file tree 2 files changed +7
-4
lines changed Original file line number Diff line number Diff line change @@ -17,8 +17,8 @@ public class Scenes {
17
17
public static void main (String [] args ) throws IOException {
18
18
String [] ln = br .readLine ().split (" " );
19
19
N = Integer .parseInt (ln [0 ]); // Total ribbon length
20
- W = Integer .parseInt (ln [1 ]);
21
- H = Integer .parseInt (ln [2 ]);
20
+ W = Integer .parseInt (ln [1 ]); // Width
21
+ H = Integer .parseInt (ln [2 ]); // Height
22
22
23
23
solution1 ();
24
24
}
@@ -51,8 +51,11 @@ static long f(int w, int ribbon) {
51
51
long scenes = 0L ;
52
52
// Try placing all possible ribbon lengths at this column/width
53
53
for (int len = 0 ; len <= H ; len ++) {
54
- scenes = (scenes + f (w + 1 , ribbon - len )) % MOD ;
54
+ scenes = (scenes + f (w + 1 , ribbon - len ));
55
55
}
56
- return dp [w ][ribbon ] = scenes ;
56
+ // Store and return the number of scenes for this sub-problem.
57
+ // Applying the MOD after the loop is safe since H is at most 100 and
58
+ // the MOD is 10^9+7 which cannot overflow a signed 64bit integer.
59
+ return dp [w ][ribbon ] = scenes % MOD ;
57
60
}
58
61
}
You can’t perform that action at this time.
0 commit comments