You are given a straight line starting at 0 to 10^9. You start at zero, and there are ‘N’ tasks you can perform. ‘ith’ task is located at point 'Task[i][0]' in the line and requires 'Task[i][1]' time to be performed.
To perform the task, you need to reach the point 'Task[i][0]' and spend 'Task[i][1]' time at that location. e.g. (5,8) lies at five, so travel distance is five, and work effort is 8.
It takes one second to travel one unit of the path.
Now, we are given a total of ‘totalTime’ seconds, and we need to complete as many tasks as possible and reach back to starting position.
Find the max number of tasks that you can finish in ‘totalTime’.
For example:
3 tasks and 16 units of total time.
Task = [( 2, 8 ) , ( 4, 5) , ( 5, 1)]
( 2, 8 ) -> task 1 at position 2 in line and takes 8 sec to complete.
( 4, 5) -> task 2 at position 4 in line and takes 5 sec to complete.
( 5, 1) -> task 3 at position 5 in line and takes 1 sec to complete.
Skipping the first task leaves us enough time to complete the other two tasks. Going to the location of the third task and coming back costs 2x5 =10 sec, and performing tasks at locations 4 and 5 cost us 5+1 = 6 sec.
Total time spent will be 10+6=16 sec.
First-line contains 'T,' denoting the number of Test cases.
For each Test case:
The first line contains two integers, 'N' and ‘totalTime’, where ‘N’ is the number of tasks and ‘totalTime’ is the time given.
Next, the ‘N’ lines contain two integers, ‘Task[i][0]’ and ‘Task[i][1]’, each denoting the distance of the task and the time required to complete the task.
Output Format:-
After operating, you must return the maximum number of tasks you can finish in time ‘totalTime’.
Note:-
You don’t need to print anything. Just implement the given function.
1<= T <=10
1<= N <=10^5
1<= totalTime <=10^9
1<= Task[i][0] <= 10^9
1<= Task[i][1] <= 10^5
Time Limit: 1 sec
2
4 11
2 3
3 5
1 4
6 1
4 20
1 1
2 1
3 1
4 1
2
4
For testcase one:
Task= [ (2,3), (3,5), (1,4), (6,1)] , totalTime= 11
In this case, we can go to position two and complete tasks one and three. This can be done in 2 * 2 + (3 + 4) = 11 sec.
Hence, the answer to this test case will be 11.
For test case two:
Task= [ (1,1), (2,1), (3,1), (4,1)] , totalTime= 20
In this case, we can go to position four and complete tasks one, two, three, and four. This can be done in 2 * 4 + (1+1+1+1) = 12 sec.
Hence, the answer to this test case will be 4.
2
3 40
6 5
5 5
3 7
1 10
10 1
3
0
For any distance ‘Task[i][0]’ try to complete tasks till Task[i][0], which takes less time first.
Approach :
Algorithm:
For every recursive call-
recur (curTask+1, n,completedTasks+1,task,remTime-task[curtask][1], max( maxDist, task[curTask][0]), answer)
recur (curTask+1, n,completedTasks,task,remTime,maxDist, answer)
Return ‘answer’
O( 2 ^ N ). Where ‘N’ is the size of the array.
We are iterating over the size ‘N’ task array and for every task, we have two choices.
Hence, the overall complexity will be O( 2^N ).
O(N), where ‘N’ is the size of the array.
We have to store the tasks.
So, the Space Complexity is O(N).