The problem goes like this:
Geek is going for n day training program. He can perform any one of these three activities Running, Fighting, and Learning Practice. Each activity has some point on each day. As Geek wants to improve all his skills, he can’t do the same activity on two consecutive days. Help Geek to maximize his merit points as you are given a 2D array of points points, corresponding to each day and activity.
Now this is how i’m trying to approach. I take each of the value of the array and recursively call on the next index while passing the last index so as to skip that in the calculation.
I’m getting it wrong because I’m returning this.currentMax
which keeps getting added to points[index][j]
whereas what I’d rather want is at each step the calculations happen for that iterations only and if it’s bigger than this.currentMax
the latter should get updated. How can I correct that? Appreciate any help.
class Solution {
//Function to find the maximum points among all the possible ones.
currentMax = -Infinity;
maximumPoints(points, n, index=0, prev=-1)
{
if(index === n) return 0;
for(var j=0; j<points[index].length; j++){
if(j !== prev){
var temp = points[index][j] + this.maximumPoints(points, n, index+1, j);
this.currentMax = Math.max(this.currentMax, temp);
}
}
return this.currentMax //This is where perhaps going wrong
}
}
var x = new Solution();
console.log(x.maximumPoints([[1,2,5], [6, 2, 10]], 2))