Skip to content

Commit 7227010

Browse files
William FisetWilliam Fiset
William Fiset
authored and
William Fiset
committed
Mountain Scenes
1 parent 5c191ba commit 7227010

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed
18.3 KB
Binary file not shown.

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,29 @@ public static void main(String[] args) throws IOException {
2727
static void solution1() {
2828
// Count the number of plain mountains. Be sure to account for having more
2929
// ribbon than can possible be placed.
30-
int plains = (Math.min(W * H, N) / W) + 1;
30+
int ribbonSquares = Math.min(W * H, N);
31+
int plains = (ribbonSquares / W) + 1;
3132

3233
dp = new Long[W + 1][N + 1];
3334
long ans = ((f(1, N) - plains) + MOD) % MOD;
3435
System.out.println(ans);
3536
}
3637

3738
static long f(int w, int ribbon) {
38-
// We're trying to place a strip of ribbon that we don't have.
39+
// We're trying to create a mountain scene for which we don't have enough ribbon material for.
3940
if (ribbon < 0) {
4041
return 0;
4142
}
42-
// Finished a mountain scene.
43+
// When the width value exceeds the frame width, we know we’ve finished creating a unique
44+
// mountain scene!
4345
if (w > W) {
4446
return 1;
4547
}
4648
if (dp[w][ribbon] != null) {
4749
return dp[w][ribbon];
4850
}
4951
long scenes = 0L;
50-
// Try all possible ribbon lengths for this column
52+
// Try placing all possible ribbon lengths at this column/width
5153
for (int len = 0; len <= H; len++) {
5254
scenes = (scenes + f(w + 1, ribbon - len)) % MOD;
5355
}

0 commit comments

Comments
 (0)