Ninja’s Training

Moderate
0/80
Average time to solve is 30m
profile
Contributed by
849 upvotes
Asked in companies
AmazonDunzoGroww

Problem statement

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.
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line of the input contains an integer, 'T,’ denoting the number of test cases.

The first line of each test case contains a single integer,' N’, denoting the number of days.

The next ‘N’ lines of each test case have 3 integers corresponding to POINTS[i].
Output Format:
For each test case, return an integer corresponding to the maximum coins  Ninja can collect.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 10
1 <= N <= 100000.
1 <= values of POINTS arrays <= 100 .

Time limit: 1 sec
Sample Input 1:
2
3
1 2 5 
3 1 1
3 3 3
3
10 40 70
20 50 80
30 60 90
Sample Output 1:
11
210
Explanation of sample input 1:
For the first test case,
One of the answers can be:
On the first day, Ninja will learn new moves and earn 5 merit points. 
On the second day, Ninja will do running and earn 3 merit points. 
On the third day, Ninja will do fighting and earn 3 merit points. 
The total merit point is 11 which is the maximum. 
Hence, the answer is 11.

For the second test case:
One of the answers can be:
On the first day, Ninja will learn new moves and earn 70 merit points. 
On the second day, Ninja will do fighting and earn 50 merit points. 
On the third day, Ninja will learn new moves and earn 90 merit points. 
The total merit point is 210 which is the maximum. 
Hence, the answer is 210.
Sample Input 2:
2
3
18 11 19
4 13 7
1 8 13
2
10 50 1
5 100 11
Sample Output 2:
45
110
Hint

Try out each combination and find the maximum sum.

Approaches (3)
Recursion

As given in the question, if we do the jth activity on day i, then Ninja cant repeat the activity on day i+1. So, we will define a recursive function REC(N,i, POINTS) that will return the maximum merit points till the Nth day, and he did the ith activity on the Nth day.

We can recursively find the value of REC(N,i, POINTS)  as  POINTS[N-1][i] + maximum among REC(N-1,i-1, POINTS) and REC(N-1,i-1, POINTS) for i different from the current choice of activity.

The final answer will be the maximum of REC(N,i, POINTS) among all three values of i.

 

Algorithm:

  • Defining 'REC'(N, i,’ POINTS’) function :
    • If N is 0:
      • No days left.
      • Return 0.
    • Set ‘ANS’ as POINTS[N-1][i-1].
    • If i == 1:
      • Set MX as maximum as REC(N-1,2,POINTS) and REC(N-1,3,POINTS).
    • If i == 2:
      • Set MX as maximum as REC(N-1,1,POINTS) and REC(N-1,3,POINTS).
    • If i == 3:
      • Set MX as maximum as REC(N-1,1,POINTS) and REC(N-1,2,POINTS).
    • Return ‘ANS’ + ‘MX’.
  • Set ‘ANS’ as 0.
  • Set ‘ANS’ as the maximum of ‘ANS’ and REC(N,1, POINTS).
  • Set ‘ANS’ as the maximum of ‘ANS’ and REC(N,2, POINTS).
  • Set ‘ANS’ as the maximum of ‘ANS’ and REC(N,3, POINTS).
  • Return ‘ANS’.
Time Complexity

O(2^N), where ‘N’ is the number of days.

 

In this approach, in the worst case, we will run the 'REC' function for all possible combinations. Each day has two choices. So the total number of combinations is 2^N. Hence the overall time complexity is O(2^N).

Space Complexity

O( 2^N), where ‘N’ is the number of days.

 

In this approach, in the worst case, we are making 2^N calls of the 'REC'() function. It will consume stack memory. Hence the overall space complexity is O(2^N).     

Video Solution
Unlock at level 3
(75% EXP penalty)
Code Solution
(100% EXP penalty)
Ninja’s Training
Full screen
Console