Schedule Tasks with the Least Starting Energy. Each pair in tasks is [actual, minimum]: a task requires at least minimum energy before starting and consumes actual energy on completion.
### Problem
Each pair in `tasks` is `[actual, minimum]`: a task requires at least minimum energy before starting and consumes actual energy on completion. Finish every task, choosing their order freely. Return the least initial energy that permits completion of them all.
```cpp
class Solution {
public:
int minimumEffort(vector<vector<int>>& tasks);
};
```
### Examples
**Example 1:**
```text
tasks = [[2,5],[3,4]]
Output: 6
Explanation: With 6, do [2,5] then [3,4], leaving 4 then 1. Starting at 5 fails in either order.
```
**Example 2:**
```text
tasks = [[1,4],[2,6],[2,2]]
Output: 6
Explanation: Do [2,6], then [1,4], then [2,2], leaving 4, 3, and 1.
```
### Constraints
- `1 <= tasks.length <= 10^5`.
- Every task has `1 <= actual <= minimum <= 10^4`.
### Clarifying Questions
Minimum is a starting threshold, not an amount consumed. Energy is never replenished during the real schedule.
```hint Inspect the structure
How do a task's starting threshold and its actual energy consumption constrain a complete schedule?
```
### Reasoning checkpoints
- Keep each task's starting threshold separate from the energy it consumes.
- Prove that the reported initial energy is sufficient and no smaller amount can complete all tasks in any order.
### Optional extensions
How would allowing energy-producing tasks change the exchange argument?
Overview: Schedule Tasks with the Least Starting Energy. Each pair in tasks is [actual, minimum]: a task requires at least minimum energy before starting and consumes actual energy on completion.
Each pair in tasks is [actual, minimum]: a task requires at least minimum energy before starting and consumes actual energy on completion. Finish every task, choosing their order freely. Return the least initial energy that permits completion of them all.
class Solution {
public:
int minimumEffort(vector<vector<int>>& tasks);
};
Examples
Example 1:
tasks = [[2,5],[3,4]]
Output: 6
Explanation: With 6, do [2,5] then [3,4], leaving 4 then 1. Starting at 5 fails in either order.
Example 2:
tasks = [[1,4],[2,6],[2,2]]
Output: 6
Explanation: Do [2,6], then [1,4], then [2,2], leaving 4, 3, and 1.
Constraints
1 <= tasks.length <= 10^5
.
Every task has
1 <= actual <= minimum <= 10^4
.
Clarifying Questions Guidance
Minimum is a starting threshold, not an amount consumed. Energy is never replenished during the real schedule.
Reasoning checkpoints
Keep each task's starting threshold separate from the energy it consumes.
Prove that the reported initial energy is sufficient and no smaller amount can complete all tasks in any order.
Optional extensions
How would allowing energy-producing tasks change the exchange argument?