Skip to content

Commit c3e4203

Browse files
Create NINJASTRAININGDPSTRIVER.java
1 parent fc4eb7b commit c3e4203

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

NINJASTRAININGDPSTRIVER.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import java.util.*;
2+
public class Solution {
3+
public static int ninjaTraining(int n, int points[][]) {
4+
int[][] dp = new int[n][4];
5+
for (int[] d : dp) {
6+
Arrays.fill(d, -1);
7+
}
8+
return maxHelper(n - 1, 3, points, dp);
9+
}
10+
11+
public static int maxHelper(int day, int last, int[][] points, int[][] dp) {
12+
if (dp[day][last] != -1) {
13+
return dp[day][last];
14+
}
15+
if (day == 0) {
16+
int maxi = 0;
17+
for (int i = 0; i <= 2; i++) {
18+
if (i != last) {
19+
maxi = Math.max(maxi, points[day][i]);
20+
}
21+
}
22+
dp[day][last] = maxi;
23+
return dp[day][last];
24+
}
25+
int maxi = 0;
26+
for (int i = 0; i <= 2; i++) {
27+
if (i != last) {
28+
int maxPoints = maxHelper(day - 1, i, points, dp) + points[day][i];
29+
maxi = Math.max(maxi, maxPoints);
30+
}
31+
}
32+
dp[day][last] = maxi;
33+
return dp[day][last];
34+
}
35+
}

0 commit comments

Comments
 (0)