Input: ‘N’ = 4, ‘NUMS’ = {2, 1, -2, 3}, ‘K’ = 1.
Output: 6.
You are standing on the stone with index 1. You can jump from stone with index 1 to stones with index 2 or 3. Jump to stone 3 and collect the value of stone 3, total score = 3.
Now you cannot make any forward jump, your friend starts his backward turn now, he can jump to stone 1 or stone 2. Jump to stone 1 and collect the value of stone 1, total score = 4.
Jump from stone 1 to stone 0 and collect the value of stone 0, total score = 6.
Hence, the maximum score that you and your friend can collect is 6.
The first line will contain the integer 'T', denoting the number of test cases.
The first line of each test case contains an integer ‘N’ and ‘K’ denoting the length of the array ‘NUMS an arbitrary integer.
The second line of each test case contains ‘N’ space-separated integers.
For each test case, you don’t need to print anything just return the maximum value you and your friend can collect.
You don't need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^6
-1000 <= NUMS[ i ] <= 1000
Sum of N Over all the Test cases <= 10^6
Time Limit: 1 sec
In this approach, we will use recursion to solve the problem. If you are standing at any stone then you can either jump 1 or 2 steps forward or stop there and let your friend jump backward. We can recursively explore all the possible moves available to you and your friend and then finally calculate the maximum sum.
Let us define our recursive function as ‘F’, the parameters on which this function depends are (index of the stone, who is on that stone you or your friend). We can use a flag variable to determine who is on the stone. 0 means you are on the stone and 1 means your friend is on the stone. Now the recurrence relation can be given as:
If flag = 0 then
F(idx, flag) = maximum of
‘NUMS[ idx ]’ + max( F(idx+1, 0), F(idx + 2, 0)) // i.e move forward.
and
‘NUMS[ idx ]’ + max( F(idx-1, 1), F(idx -2, 1)) // i.e move backward.
else
F(idx, flag) = ‘NUMS[ idx ]’ + max( F(idx-1, 1), F(idx -2, 1)) // i.e move backward
In this approach, the idea is to optimize the previous approach using memoization, if we observe carefully we are calculating the value of any ‘F’(idx, flag) more than once. If we can store the value of it once we have calculated it then every time this state of function will be called again then
In this approach, we can calculate the maximum score for each stone that we can collect while jumping forward. It can be easily found by the relation. ‘DP[ i ]’ = max( DP[ i - 1 ], DP[ i - 2 ] ) + ‘NUMS[ i ]’. We can use this to find the maximum score we can collect while jumping backward. For each ‘i’th stone, the maximum score can be obtained from 3 conditions:
i. If we are jumping from ( ‘i’ + 1 )th stone to ‘i’th stone, i.e, ‘DP[ i + 1 ] + ‘NUMS [ i ].
ii. Or if we are jumping from ( ‘i’ + 2 )th stone to ‘i’th stone, i.e, DP[ i + 2 ] + ‘NUMS[ i ]’.
iii. Or, the forward was stopped on the ‘i’th stone and the friend is just starting from that stone, i.e, DP [ i ].
The maximum of these three conditions gives us the maximum score we can get for the ‘i’th stone.