Max Budget || Task Completion ||

You are given a straight line statring at 0 to 10^9. You start at zero and there are n tasks you can perform. ith task is located at point 'i' in the line and requries 't' time to be performed. To perform the task you need to reach the ppoint 'i' and spend 't' time at that location. e.g (5,8) lies at 5 so travel distance is 5 and work effort is 8.
Total effort is calculated as travel distance + time required to complete the work.

It takes one sec to travel one unit of path.

Now we are given total T 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 time T.
e.g
3 16 -> 3 tasks and 16 units of total time
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
​​​​​​​
Output : 2

Explanation :
If we take task 1 at location 2 which requires 8 sec then getting to location 2 takes 2s and completing the task takes 8s leaving us with only 6s which is not enough for completing other task

On the other hand skiiping the fist taks leaves us enough time to complete the other two tasks.

Going to location and coming back costs 2x5 =10s and performing task at location 4 and 5 cost us 5+1 = 6s. Total time spent will be 10s+6s=16s.

Can anyone please review this? What I am missing, I have explanined my solution as under.

For solving this I have implemented following code. It is passing all the test cases except 4. I am not able to figure out what is missing in this code. I tried adding multiple custom input but it is not failing, so that I can get which edge case missing.

Intution for the below code : I am following greedy apporach. since brute force will take 2^n times
0) sort task as per start time

  1. Keep adding amount of work that need to be done in the heap, if it is possible to do so.
  2. if it is not possible to add current item in heap, check if current max in the heap is smaller then amount of work need to be done for current task, if it is then there in no point adding current task into heap by remove existing task from heap.
  3. if current is smaller that current max in heap that keep popping until we have space for adding current element in the heap.
    public static int maxTask(int tasks[][],int t) {
        int maxTasks = 0;
        Arrays.sort(tasks, (r1,r2)->Integer.compare(r1[0],r2[0]));
        PriorityQueue<Integer> queue = new PriorityQueue<>(Collections.reverseOrder());
        for(int i=0;i<tasks.length;i++) {
            if(t-(2*tasks[i][0])-tasks[i][1]>=0) {
                queue.add(tasks[i][1]);
                t-=tasks[i][1];
                maxTasks = Math.max(maxTasks,queue.size());
            }else if((!queue.isEmpty() && queue.peek()<=tasks[i][1])) {
                continue;
            }
            else {
                    while (!queue.isEmpty() && (t-2*tasks[i][0]-tasks[i][1])<0) {
                        int maXConsumed = queue.remove();
                        t+=maXConsumed;
                    }
                    if(t-2*tasks[i][0]-tasks[i][1]>0) {
                        queue.add(tasks[i][1]);
                        maxTasks = Math.max(maxTasks,queue.size());
                    }
            }
        }
        return maxTasks;
    }
Comments (0)