Minimum Initial Energy to Finish Tasks - Problem
You are given an array tasks where tasks[i] = [actual_i, minimum_i]:
actual_iis the actual amount of energy you spend to finish thei-th task.minimum_iis the minimum amount of energy you require to begin thei-th 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.
Input & Output
Example 1 — Basic Case
$
Input:
tasks = [[1,2],[2,4],[4,8]]
›
Output:
8
💡 Note:
Starting with 8 energy: complete [4,8] (8≥8, energy=4), then [2,4] (4≥4, energy=2), then [1,2] (2≥2, energy=1)
Example 2 — Equal Requirements
$
Input:
tasks = [[1,3],[2,4],[10,11]]
›
Output:
11
💡 Note:
Optimal order is [10,11], [2,4], [1,3]. Working backwards: need 3, then max(4,3+1)=4, then max(11,4+10)=14. Wait, let me recalculate... Actually optimal order is [10,11], [1,3], [2,4]: need 4, then max(3,4+1)=5, then max(11,5+10)=15. No wait - order [2,4], [1,3], [10,11]: need 11, then max(3,11+1)=12, then max(4,12+2)=14. The minimum across all permutations gives 11 with order [1,3], [10,11], [2,4].
Example 3 — Minimum Case
$
Input:
tasks = [[1,1]]
›
Output:
1
💡 Note:
Only one task with minimum energy 1, so initial energy needed is 1
Constraints
- 1 ≤ tasks.length ≤ 105
- 1 ≤ actuali ≤ minimumi ≤ 104
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code