Skip to content

Commit d3bbe04

Browse files
committed
ninja training
1 parent f321827 commit d3bbe04

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

leetcodeQuestions/ninjaTraining.java

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import java.util.Arrays;
2+
3+
public class Solution {
4+
static int dp[][];
5+
public static int count(int [][] points ,int n , int prev){
6+
int maxi=0;
7+
if( n==0){
8+
for( int i=0 ;i<3;i++){
9+
if(i!=prev){
10+
11+
maxi=Math.max(maxi, points[0][i]);
12+
}
13+
14+
15+
}
16+
17+
18+
19+
return maxi;
20+
}
21+
22+
if(dp[n][prev]!=-1) return dp[n][prev];
23+
24+
25+
for( int i=0 ;i< 3 ;i++){
26+
27+
if(i!=prev){
28+
29+
int earn = points[n][i] + count(points, n-1, i);
30+
31+
32+
maxi=Math.max(maxi, earn);
33+
34+
35+
36+
37+
}
38+
39+
40+
41+
42+
}
43+
44+
return dp[n][prev]= maxi;
45+
46+
47+
48+
49+
50+
51+
52+
}
53+
public static int ninjaTraining(int n, int points[][]) {
54+
dp= new int [points.length][4];
55+
for( int i=0 ;i<dp.length;i++){
56+
Arrays.fill(dp[i], -1);
57+
}
58+
return count(points,n-1,3);
59+
60+
// Write your code here..
61+
}
62+
63+
}

0 commit comments

Comments
 (0)