-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ninja's training
50 lines (41 loc) · 1.07 KB
/
Ninja's training
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Bruth Force Approach -> Recursion + Memoization
// TC - O(2^N)
// SC - O(N) Stack Space
import java.util.*;
public class Solution {
public static int ninjaTraining(int n, int points[][]) {
// Write your code here..
int[][] dp = new int[n][4];
for (int[] x : dp) {
Arrays.fill(x, -1);
}
return helper(n-1, points, 0, dp);
}
public static int helper
(
int n,
int[][] points,
int check,
int[][] dp
) {
if (n < 0) {
return 0;
}
if (dp[n][check] != -1) {
return dp[n][check];
}
int first = 0;
int second = 0;
int third = 0;
if (check != 1) {
first = points[n][0] + helper(n-1, points, 1, dp);
}
if (check != 2) {
second = points[n][1] + helper(n-1, points, 2, dp);
}
if (check != 3) {
third = points[n][2] + helper(n-1, points, 3, dp);
}
return dp[n][check] = Math.max(first, Math.max(second, third));
}
}