
The first line of the input contains an integer, 'T,’ denoting the number of test cases.
The first line of each test case contains an integer 'N', denoting the index of the last checkpoint.
The second line of each test case contains 'N' space-separated integers denoting the first column of the matrix 'TIME'.
The third line of each test case contains 'N' space-separated integers denoting the second column of the matrix 'TIME'.
The fourth line of each test case contains 'N-1' space-separated integers denoting the first column of the matrix 'CHECKPOINTS'.
The fifth line of each test case contains 'N-1' space-separated integers denoting the second column of the matrix 'CHECKPOINTS'.
For each test case, return the minimum time it will take for Mr. X to reach Ninjaland.
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^5
1 <= TIME[i][j] , CHECKPOINTS[i][j] <= 10^3
Time Limit: 1 sec
The idea is to use a recursive approach to find the minimum time to reach Ninjaland. The recursive idea is very clear that for any checkpoint at any of the two lanes, there are only two choices, either continue moving along the same lane or switch between the two lanes. Using this idea we can calculate the minimum travel time recursively.
We will define a function GET_MIN_TIME(i,j) to find the minimum time it takes to reach Checkpoint i at Lane-j. After defining the function, we can call it to find the Minimum Time it takes to reach Checkpoint ‘N’, at Lane-0 and Lane-1. Our final answer will be the Minimum among the two values.
In the previous approach, the function 'GET_MIN_TIME'(i,j) is recursively being calculated several times for some fixed values of i and j. So, here we can use the technique of 'memoization to optimize the working of the previous approach. We will create a 2D Matrix to store all the previously computed values, so that no value is being calculated multiple times. We will make a 2D array 'MEMO' having N+1 rows and 2 columns with all values initialized as -1, 'MEMO'[i][j] will denote the minimum time to reach the Checkpoint i at Lane-j. Note that the working here is the same as the previous approach, just that we will modify the recursive function, so that it does not calculate the same answer twice.
In the first approach, the function 'GET_MIN_TIME' recursively calculates the time it takes to cross all the checkpoints which has many overlapping subproblems. i.e. a recursive function computes the same sub-problems again and again. So we can use Dynamic Programming to overcome the overlapping subproblems. So we will convert the 'GET_MIN_FUNCTION' to a 2-D array having N and 2 columns to store the DP states. Let 'GET_MIN_TIME[i][j]’ denotes the minimum Time to reach Checkpoint ‘i + 1’ at Lane ‘j’.
The recursive relation to find out 'GET_MIN_TIME[i][j]’ can be written as :
Taking the base case as 'GET_MIN_TIME[0][0]’ = ‘TIME[0][0]’ and 'GET_MIN_TIME[0][1]’ = ‘TIME[0][1]’.
If we observe carefully, we are finding the value of 'GET_MIN_TIME[i][j]’ in the same manner we were finding values using the function 'GET_MIN_TIME(i,j)’. The major difference is that we have stored all our previously computed values to save redundancy.
This approach works fine but we are using an extra 2-D array, we can also optimize that if we observe that to compute the i'th row we only need the values of (i-1)'th row. So, there is no need to store all the previous values. We can just store the previous row values for our calculations and update them as we move ahead.