Skip to content

Commit 52cb641

Browse files
William FisetWilliam Fiset
William Fiset
authored and
William Fiset
committed
Mountain scene slides
1 parent dbce890 commit 52cb641

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed
34.8 KB
Binary file not shown.

src/main/java/com/williamfiset/algorithms/dp/examples/scenes/Scenes.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ public class Scenes {
1717
public static void main(String[] args) throws IOException {
1818
String[] ln = br.readLine().split(" ");
1919
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
2222

2323
solution1();
2424
}
@@ -51,8 +51,11 @@ static long f(int w, int ribbon) {
5151
long scenes = 0L;
5252
// Try placing all possible ribbon lengths at this column/width
5353
for (int len = 0; len <= H; len++) {
54-
scenes = (scenes + f(w + 1, ribbon - len)) % MOD;
54+
scenes = (scenes + f(w + 1, ribbon - len));
5555
}
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;
5760
}
5861
}

0 commit comments

Comments
 (0)