The start time of one job can be equal to the end time of another.
The first line contains an integer 'T' denoting the number of test cases to be run.
The first line of each test case contains a single integers 'N' denoting the number of jobs.
The second line of each test case contains ‘N’ single space-separated integers denoting the start time of 'N' jobs respectively.
The third line of each test case contains ‘N’ single space-separated integers denoting the end time of 'N' jobs respectively.
The fourth line of each test case contains ‘N’ single space-separated integers denoting the profit of 'N' jobs respectively.
For each test case, the maximum profit is printed.
Print the output of each test case in a separate line.
Can you solve this problem in O(N*log(N)) time complexity?
1 <= T <= 100
1 <= N <= 3000
1 <= Start[i] < End[i] <= 10^9
1 <= Profit[i] <= 10^9
Where 'T' denotes the number of test cases, 'N' denotes the number of jobs respectively, 'Start[i]' and 'End[i]' denotes the start and end time of 'i-th' job, and 'Profit[i]' denotes the profit of 'i-th' job.
We are solving this problem by solving its subproblems and then combining the solutions of those subproblems. If we analyze carefully, we will see that we are solving the same subproblems multiple times. Thus, this problem exhibits overlapping subproblems. This means, in this approach, we will eliminate the need for solving the same subproblems again and again.
Suppose the Job array after sorting is = [ {1, 2, 5}, {3, 4, 10}, {3, 6, 9}, {5, 7, 6}]. The recursion tree for this is:
As we can see:
To optimize the overlapping sub-problem calculation, we will use memoization by storing answers for each recursive state.
The idea is the same as the previous approach, but in the previous approach many recursive calls were made again and again with the same parameters. This can be eliminated by storing the value obtained from a particular call in a 1d array, called the lookUp array. lookUp[i] stores the maximum profit obtained by performing jobs till ith index.
Algorithm:
This approach relies on the fact that the maximum profit possible up to ith index is independent of the jobs coming after the ith index. Thus, for every index we will store the maximum profit from jobs upto that index in an array, say DP array. For every index there will be two cases :
Algorithm:
Negative To The End
Sort 0s, 1s, 2s
Day 28 : Fake Coin Problem
Day 28 : Fake Coin Problem
Search In A Rotated Sorted Array II
Find Duplicate in Array