Minimum Initial Energy to Finish Tasks
You are given an array tasks where tasks[i] = [actuali, minimumi]:
actualiis the actual amount of energy you spend to finish thei^thtask.minimumiis the minimum amount of energy you require to begin thei^thtask.
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
8Starting 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
Input
tasks = [[1,3],[2,4],[10,11],[10,12],[8,9]]Output
32Starting 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