Minimum Initial Energy to Finish Tasks

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

  • actuali is the actual amount of energy you spend to finish the i^th task.
  • minimumi 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.

Example 1
Inputtasks = [[1,2],[2,4],[4,8]]
Output8
Starting with 8 energy, you can complete the 3rd, 2nd, and 1st tasks in that order, while starting with 7 energy does not work because you cannot begin the 3rd task.
Example 2
Inputtasks = [[1,3],[2,4],[10,11],[10,12],[8,9]]
Output32
Starting with 32 energy, you can complete the tasks in order from 1st to 5th and have 1 energy left after finishing all tasks.

Constraints

  • 1 <= tasks.length <= 10^5
  • 1 <= actuali <= minimumi <= 10^4

Asked at 5 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate