Maximum Total Reward from a Contiguous Task Block

You are given a 2D integer array tasks of size n, where tasks[i] = [durationi, deadlinei]. each task takes durationi units of time to complete and has a deadline deadlinei. You must choose exactly one contiguous subarray of tasks and process only those tasks. The selected tasks must be processed in the same order as they appear in the array, and the starting time is 0.

For a task, let its finishing time be the time at which its execution ends.

The reward earned from that task is: deadlinei - finishingTime

Note that the reward can be negative.

Return the maximum total reward you can achieve by choosing the best contiguous subarray of tasks.

Example 1:

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

Output: 10

Explanation:

Optimal contiguous subarray is [[3,10],[1,5],[2,8]].

  • Process [3,10] → finishing time = 3 → reward = 10 - 3 = 7
  • Process [1,5] → finishing time = 4 → reward = 5 - 4 = 1
  • Process [2,8] → finishing time = 6 → reward = 8 - 6 = 2

Total reward = 7 + 1 + 2 = 10

Example 2:

Input: tasks = [[4,7],[2,10],[3,9]]

Output: 12

Explanation:

Choose the contiguous subarray [[2,10],[3,9]].

  • Process [2,10] → finishing time = 2 → reward = 10 - 2 = 8
  • Process [3,9] → finishing time = 5 → reward = 9 - 5 = 4

Total reward = 8 + 4 = 12

Now Your Turn!

Pick the correct output for the given input

Input: tasks = [[2,6],[1,4],[3,9]]

Still unsure what the problem is asking ?

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

Constraints:

  • 1 <= n <= 2 * 105
  • tasks.length == n
  • tasks[i].length == 2
  • 1 <= durationi, deadlinei <= 106

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
long long maximumReward(vector<vector<int>> &tasks) {
// Your code goes here
}
};
Test Case

Input:

Tasks