Minimum Initial Energy to Finish Tasks - Problem

You are given an array tasks where tasks[i] = [actual_i, minimum_i]:

  • actual_i is the actual amount of energy you spend to finish the i-th task.
  • minimum_i is the minimum amount of energy you require to begin the i-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
Minimum Initial Energy to Finish Tasks INPUT tasks = [[1,2],[2,4],[4,8]] Task 0: [1, 2] actual=1, minimum=2 diff = 2-1 = 1 Task 1: [2, 4] actual=2, minimum=4 diff = 4-2 = 2 Task 2: [4, 8] actual=4, minimum=8 diff = 8-4 = 4 Energy Requirements 2 4 8 ALGORITHM STEPS 1 Sort by (min - actual) Descending: larger diff first Sorted: [4,8], [2,4], [1,2] diff: 4, 2, 1 2 Process Task [4,8] Need 8, spend 4, left: 4 3 Process Task [2,4] Need 4, spend 2, left: 2 4 Process Task [1,2] Need 2, spend 1, left: 1 Energy Flow 8 --> 4 --> 2 --> 1 Greedy Strategy: Do hardest tasks first! FINAL RESULT Output 8 OK - All tasks completed! Execution Trace Start: energy = 8 Task [4,8]: 8 >= 8 OK 8 - 4 = 4 remaining Task [2,4]: 4 >= 4 OK 4 - 2 = 2 remaining Task [1,2]: 2 >= 2 OK 2 - 1 = 1 remaining Final: energy = 1 Key Insight: Sort tasks by (minimum - actual) in descending order. Tasks with larger gaps between minimum and actual cost should be done first. This greedy approach ensures we always have enough energy when starting difficult tasks, minimizing the total initial energy needed. TutorialsPoint - Minimum Initial Energy to Finish Tasks | Greedy Sorting Approach
Asked in
Google 15 Amazon 12 Microsoft 8
18.5K Views
Medium Frequency
~25 min Avg. Time
485 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen