Tasks and Deadlines

You are given a 2D integer array tasks where tasks[i] = [durationi, deadlinei]. You must process all tasks sequentially starting at time 0.

The reward for completing a task is its deadline minus your finishing time for that task. Return the maximum total reward you can achieve by choosing the optimal processing order.

Example 1:

Input: tasks = [[6, 10], [8, 15], [5, 12]]

Output: 2

Explanation:

Optimal order is by duration: (5, 6, 8).

Finish times are 5, 11, and 19.

Total reward = (12-5) + (10-11) + (15-19) = 2.

Example 2:

Input: tasks = [[2, 10], [4, 5]]

Output: 7

Explanation:

Optimal order is by duration: (2, 4).

Finish times are 2 and 6.

Total reward = (10-2) + (5-6) = 7.

Now Your Turn!

Pick the correct output for the given input

Input: tasks = [[4, 10], [2, 8], [1, 5], [3, 12]]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= tasks.length <= 2 x 105
  • tasks[i].length == 2
  • 1 <= duration, deadline <= 106

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
long long TasksAndDeadlines(vector<vector<int>>& tasks) {
}
};
Test Case

Input:

Tasks