Formatted question description: https://leetcode.ca/all/1665.html

# 1665. Minimum Initial Energy to Finish Tasks (Hard)

You are given an array tasks where tasks[i] = [actuali, minimumi]:

• actuali is the actual amount of energy you spend to finish the ith task.
• minimumi is the minimum amount of energy you require to begin the ith task.

For example, if the task is [10, 12] and your current energy is 11, you cannot start this task. However, if your current energy is 13, you can complete this task, and your energy will be 3 after finishing it.

You can finish the tasks in any order you like.

Return the minimum initial amount of energy you will need to finish all the tasks.

Example 1:

Input: tasks = [[1,2],[2,4],[4,8]]
Output: 8
Explanation:
Starting with 8 energy, we finish the tasks in the following order:
- 3rd task. Now energy = 8 - 4 = 4.
- 2nd task. Now energy = 4 - 2 = 2.
- 1st task. Now energy = 2 - 1 = 1.
Notice that even though we have leftover energy, starting with 7 energy does not work because we cannot do the 3rd task.

Example 2:

Input: tasks = [[1,3],[2,4],[10,11],[10,12],[8,9]]
Output: 32
Explanation:
Starting with 32 energy, we finish the tasks in the following order:
- 1st task. Now energy = 32 - 1 = 31.
- 2nd task. Now energy = 31 - 2 = 29.
- 3rd task. Now energy = 29 - 10 = 19.
- 4th task. Now energy = 19 - 10 = 9.
- 5th task. Now energy = 9 - 8 = 1.

Example 3:

Input: tasks = [[1,7],[2,8],[3,9],[4,10],[5,11],[6,12]]
Output: 27
Explanation:
Starting with 27 energy, we finish the tasks in the following order:
- 5th task. Now energy = 27 - 5 = 22.
- 2nd task. Now energy = 22 - 2 = 20.
- 3rd task. Now energy = 20 - 3 = 17.
- 1st task. Now energy = 17 - 1 = 16.
- 4th task. Now energy = 16 - 4 = 12.
- 6th task. Now energy = 12 - 6 = 6.


Constraints:

• 1 <= tasks.length <= 105
• 1 <= actual​i <= minimumi <= 104

Related Topics:
Greedy

## Solution 1. Greedy

Let d[i] = A[i][1] - A[i][0], i.e. the difference between the minimum and actual energy for task i.

We should take tasks with greater d value first because in this way we can save more energy for later use.

// OJ: https://leetcode.com/problems/minimum-initial-energy-to-finish-tasks/

// Time: O(NlogN)
// Space: O(1)
class Solution {
public:
int minimumEffort(vector<vector<int>>& A) {
int N = A.size(), ans = 0, cur = 0;
sort(begin(A), end(A), [&](auto &a, auto &b) { return (a[1] - a[0]) > (b[1] - b[0]); });
for (int i = 0; i < N; ++i) {
if (cur < A[i][1]) {
ans += A[i][1] - cur;
cur = A[i][1];
}
cur -= A[i][0];
}
return ans;
}
};


## Solution 2.

Sort the array in ascending order of minimum - actual.

Take the tasks one by one, the minimum effort for A[0 .. i] is max(ans + A[i][0], A[i][1]).

// OJ: https://leetcode.com/problems/minimum-initial-energy-to-finish-tasks/

// Time: O(NlogN)
// Space: O(1)
class Solution {
public:
int minimumEffort(vector<vector<int>>& A) {
int ans = 0;
sort(begin(A), end(A), [](auto &a, auto &b) { return (a[1] - a[0]) < (b[1] - b[0]); });
for (auto &a : A) ans = max(ans + a[0], a[1]);
return ans;
}
};


Java

class Solution {
}
});
int minEnergy = 0;
int sum = 0;