File tree 2 files changed +6
-4
lines changed
slides/dynamicprogramming
src/main/java/com/williamfiset/algorithms/dp/examples/scenes
2 files changed +6
-4
lines changed Original file line number Diff line number Diff line change @@ -27,27 +27,29 @@ public static void main(String[] args) throws IOException {
27
27
static void solution1 () {
28
28
// Count the number of plain mountains. Be sure to account for having more
29
29
// 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 ;
31
32
32
33
dp = new Long [W + 1 ][N + 1 ];
33
34
long ans = ((f (1 , N ) - plains ) + MOD ) % MOD ;
34
35
System .out .println (ans );
35
36
}
36
37
37
38
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 .
39
40
if (ribbon < 0 ) {
40
41
return 0 ;
41
42
}
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!
43
45
if (w > W ) {
44
46
return 1 ;
45
47
}
46
48
if (dp [w ][ribbon ] != null ) {
47
49
return dp [w ][ribbon ];
48
50
}
49
51
long scenes = 0L ;
50
- // Try all possible ribbon lengths for this column
52
+ // Try placing all possible ribbon lengths at this column/width
51
53
for (int len = 0 ; len <= H ; len ++) {
52
54
scenes = (scenes + f (w + 1 , ribbon - len )) % MOD ;
53
55
}
You can’t perform that action at this time.
0 commit comments