File tree Expand file tree Collapse file tree 2 files changed +4
-4
lines changed
slides/dynamicprogramming
src/main/java/com/williamfiset/algorithms/dp/examples/magicalcows Expand file tree Collapse file tree 2 files changed +4
-4
lines changed Original file line number Diff line number Diff line change @@ -43,16 +43,16 @@ public static void main(String[] args) throws IOException {
43
43
dp [0 ][cows ]++;
44
44
}
45
45
46
- for (int day = 1 ; day <= MAX_DAYS ; day ++) {
46
+ for (int day = 0 ; day < MAX_DAYS ; day ++) {
47
47
// For all farm sizes between 1 and `C`, double the number of cows.
48
48
for (int i = 1 ; i <= C ; i ++) {
49
- if (2 * i <= C ) {
49
+ if (i <= C / 2 ) {
50
50
// Cow count on farm with size `i` doubled, but the number of farms did not.
51
- dp [day ][ 2 * i ] += dp [day - 1 ][i ];
51
+ dp [day + 1 ][ i * 2 ] += dp [day ][i ];
52
52
} else {
53
53
// The number of cows per farm on the farm with size `i` exceeds the
54
54
// permitted limit, so double the number of farms.
55
- dp [day ][i ] += 2 * dp [day - 1 ][i ];
55
+ dp [day + 1 ][i ] += 2 * dp [day ][i ];
56
56
}
57
57
}
58
58
}
You can’t perform that action at this time.
0 commit comments