Skip to content

Commit 513d082

Browse files
committed
DP Strategy Game 1 - initial commit
1 parent 160c00f commit 513d082

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

src/main/java/codechallenges/dynamicprogramming/StrategyGameOne.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,30 @@
2020
public class StrategyGameOne {
2121

2222
public int solve(int[] hitPoints) {
23-
return 0;
23+
24+
int sum = 0;
25+
for (int hp : hitPoints ) {
26+
sum += hp;
27+
}
28+
29+
int[] hits = new int[sum + 1];
30+
hits[0] = 0;
31+
32+
for (int i = 1; i <= sum; i++) {
33+
34+
hits[i] = hits[i - 1] + 1;
35+
36+
if (i - 9 >= 0 && hits[i - 9] < hits[i]) {
37+
hits[i] = hits[i - 9] + 1;
38+
}
39+
40+
if (i - 3 >= 0 && hits[i - 3] < hits[i]) {
41+
hits[i] = hits[i - 3] + 1;
42+
}
43+
44+
}
45+
46+
return hits[sum];
2447
}
2548

2649
int hitPoints(int current) {

0 commit comments

Comments
 (0)