Ninja is planing this ‘N’ days-long training schedule. Each day, he can perform any one of these three activities. (Running, Fighting Practice or Learning New Moves). Each activity has some merit points on each day. As Ninja has to improve all his skills, he can’t do the same activity in two consecutive days. Can you help Ninja find out the maximum merit points Ninja can earn?
You are given a 2D array of size N*3 ‘POINTS’ with the points corresponding to each day and activity. Your task is to calculate the maximum number of merit points that Ninja can earn.
For Example
If the given ‘POINTS’ array is [[1,2,5], [3 ,1 ,1] ,[3,3,3] ],the answer will be 11 as 5 + 3 + 3.
Constraints:
1 <= T <= 10
1 <= N <= 100000.
1 <= values of POINTS arrays <= 100 .
Time limit: 1 sec
Initialization: We use two 1D arrays, prev and curr, to store the intermediate results. The prev array represents the results for the day after the current day, and curr represents the results for the current day.
Base Case: Initialize prev with zeros since, on the last day, the maximum points for the next day is zero (there is no next day).
Dynamic Programming Transition: Iterate from the last day to the first day. For each day, calculate the maximum points for each activity considering the constraints: - If the activity is not performed on the previous day, add the points for that activity to the points of the next day. - Store the maximum of these values in the curr array.
Update prev: After processing each day, update prev to be a clone of curr for the next iteration.
Result: After the loop, the prev array contains the maximum points starting from any activity on the first day. Find the maximum value in prev to get the final result. This approach reduces the space complexity from (O(n * 3)) to (O(3) ~ O(1)) while maintaining the same time complexity of (O(n *3)).