The first line contains an integer ‘T’ denoting the number of test cases. Then each test case is as follows.
The first input line of each test case contains an integer ‘N’ which denotes the number of pairs.
Next ‘N’ lines contain two space-separated integers denoting a pair.
For each test case, print the length of the longest chain which can be formed.
Print the output of each test case in a separate line.
You are not required to print the expected output; it has already been taken care of. Just implement the function.
1 <= T <= 50
1 <= N <= 1000
0 <= pairs[i][0], pairs[i][1] <= 10^6
Time limit: 1 sec
The basic idea of this approach is to break the original problem into sub-problems. Let us sort the array/list of pairs by the first integer in non-decreasing order and define a recursive function
getLength(int i)
Which returns the length of the longest chain which ends at pairs[i]. As per given in the problem statement pair[i] can follow a pair[j] (for any integer ‘j’) if pairs[i][0] > pairs[j][1]. Since the array/list is sorted in nondecreasing order and also it is given that the second integer is always greater than the first integer in any pair, ‘j’ must be less than ‘i’.
Therefore, we can define getLength(i) = max(getLength(i), getLength(j) + 1) if j < i and ( pairs[i][0] > pairs[j][1] ) holds true.
Now, consider the following steps to implement getLength(i) :
After observing approach 1 we can find that there are some redundant function calls which means that there are some overlapping subproblems. The repetition of such sub-problems suggests that we can use dynamic programming to optimize our approach.
The key idea behind a dynamic programming approach is to use memoization, i.e. we’ll save the result of our sub-problem in a matrix so that it can be used later on.
Let dp[ i ] be our dynamic programming array to store the length of the longest chain ending at ith pair.
Now consider the following implementation of getLength(int i) using memoization.
The basic idea here is to use a bottom-up dynamic programming approach to solve the problem. The recursion function is discussed in the above-mentioned approach.
Let dp[ i ] be our dynamic programming array to store the length of the longest chain ending at ith pair.
Now, consider the following steps :
The basic idea of this approach is to greedily add pairs to the chain. We can observe that the chain will always be sorted by the second integer in increasing order.
Why??
Because the first integer of any pair will be greater than the second integer of the previous one. And since it is given that the first integer in any pair is always less than the second one. So, the second integer will always be greater than the second integer of the previous pair.
Now our strategy is to choose the next valid pair with the minimum second integer.
Let us try to prove the above-mentioned greedy strategy always gives the optimal result.
Suppose we currently have a chain of ‘K’ length and the last pair in the chain is (a, b).
Now assume that we are not choosing the pair with a minimum second integer, let say the chosen pair is (c, d) where c > b. Now there must exist a pair (e, f) such that e > a and f < d.
Since we have already shown that the chain will be sorted by its second element in increasing order. So the pair (e, f) can never be added to the chain again. So, it is always better to choose the next valid pair with the minimum second integer.
Now, consider the following steps: