Minimum time to cross all checkpoints

Hard
0/120
Average time to solve is 15m
profile
Contributed by
7 upvotes
Asked in company
Barclays

Problem statement

Mr. X is a busy person. He wants to reach Ninjaland as soon as possible. Mr. X's house and Ninjaland are connected by a straight road having two parallel lanes. Let the two lanes be Lane-0 and Lane-1. He can start from any of the two lanes, and during the course of the journey, he can switch between the two lanes. There are a total of 'N+1' checkpoints on the road. Let the checkpoints be numbered from 0 to 'N'. Mr. X, is currently at Checkpoint 0 and Ninjaland is at checkpoint 'N'. Note that, it is only possible to switch between the lanes when the car is present at a checkpoint.

The time it takes to move between two consecutive checkpoints is given in the 2D Matrix 'TIME' having 'N' rows and 2 columns. 'TIME[i][j]' denotes the time it takes to move from 'i'th' checkpoint to the '(i+1)'th' checkpoint by traveling through Lane - j.

The time it takes to switch lanes at checkpoints is given in the 2D Matrix 'CHECKPOINTS' having 'N-1' rows and 2 columns. If Mr. X switches from Lane-0 to Lane-1 at Checkpoint 'i+1', then he reaches Lane-1 at Checkpoint 'i+2' and it takes 'CHECKPOINTS[i][0]' time to complete the switch. Similarly when he switches from Lane-1 to Lane-0 at Checkpoint 'i+1', he reaches Lane-0 at Checkpoint 'i+2' and it takes 'CHECKPOINTS[i][1]' time to complete the switch. Note, that it is not possible to switch lanes at Checkpoint 0 and Checkpoint N.

Given that Mr. X can start his journey from any lane, and end his journey at any (maybe different from starting lane) lane, find the minimum time it will take for Mr. X to reach Ninjaland.

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 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'.
Output Format:
For each test case, return the minimum time it will take for Mr. X to reach Ninjaland.
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 <= 10^5
1 <= TIME[i][j] , CHECKPOINTS[i][j] <= 10^3

Time Limit: 1 sec
Sample Input 1:
2
2
2 2
3 1
2
4 
3
1 2 2
2 4 1
1 5
3 5
Sample Output 1:
4
3
Explanation for Sample Input 1:
For the first test case : 
Mr. X can start his journey at Lane-0, and does not need to switch lanes at all. The total time of travel will be 4 (2+2) in this case.

For the second test case:
Mr. X can start his journey at Lane-0 and move to Checkpoint-1 taking 1 unit of time, then switch Lanes at Checkpoint-1 taking 1 unit of time. Now, he is at Checkpoint-2 at Lane-1. Then, he will move to Checkpoint-3 taking 1 unit of time. The total travel time will be 3 (1+1+1) in this case.
Sample Input 2:
2
3
1 2 5
3 1 4
1 2
1 1
2
5 6
6 8
1 
1
Sample Output 2:
3
6
Hint

Try to think of a recursive approach to find the minimum time to cross all the checkpoints.

Approaches (3)
Recursive Approach

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.

 

Working of the Recursive function: 

  1. If i is equal to 1, then we will return TIME[0][j]. This will serve as the base case for our recursive function. Note that for Checkpoint-1 there is only 1 choice.
  2. If j is equal to 0, then we will return the minimum value among GET_MIN_TIME(i-1,0) + Time[i-1][0] and GET_MIN_TIME(i-1,1) + CHECKPOINTS[i-2][1]. As we can see that to reach Checkpoint ‘i’ at Lane-0 there are only two choices, either come to Checkpoint i-1 at Lane-0, and move along the same lane, come to Checkpoint i-1 at Lane-1, and switch lanes. So, we are comparing the two results and taking the minimum value among them.
  3. Otherwise,  we will return the minimum value among GET_MIN_TIME(i-1,1)+Time[i-1][1] and GET_MIN_TIME(i-1,0)+CHECKPOINTS[i-2][0]. As we can see that to reach Checkpoint ‘i’ at Lane-1 there are only two choices, either come to Checkpoint ‘i-1’ at Lane-1, and move along the same lane, come to Checkpoint ‘i-1’ at Lane-0, and switch lanes. So, we are comparing the two results and taking the minimum value among them.
Time Complexity

O(2^N), where ‘N’ is the index of the last checkpoint.

 

In each recursive call to the getMinFunction we are making 2 recursive calls. Therefore, for ‘N’ checkpoints, we will be calling the getMinFunction 2^N times. Hence, the overall time complexity is O(2^N).

Space Complexity

O(N),  where ‘N’ is the index of the last checkpoint.

 

The recursion depth will be ‘N’. Hence, the overall space complexity is O(N)). 

Code Solution
(100% EXP penalty)
Minimum time to cross all checkpoints
Full screen
Console