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 <= actuali <= minimumi <= 104
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The goal is to figure out the smallest amount of energy you need to start with to complete a series of tasks. The brute force method involves trying every possible starting energy level until we find one that works.
Here's how the algorithm would work step-by-step:
def minimum_initial_energy_brute_force(tasks):
initial_energy_guess = 0
while True:
current_energy = initial_energy_guess
task_completion_possible = True
# Iterate through each task to check energy sufficiency
for task_energy_requirement in tasks:
# If current energy dips below zero, we need more initial
if current_energy < task_energy_requirement:
task_completion_possible = False
break
current_energy -= task_energy_requirement
# We found the minimum energy.
if task_completion_possible:
return initial_energy_guess
# If initial energy insufficient, increment and retry.
initial_energy_guess += 1
To find the minimum starting energy needed, we'll work backward, focusing on the trickiest tasks first. We want to make sure we can complete all tasks without running out of energy, even if it means starting with a bit more than the energy each task needs individually.
Here's how the algorithm would work step-by-step:
def minimum_initial_energy(tasks):
minimum_energy_needed = 0
current_energy = 0
for cost, required in reversed(tasks):
if current_energy < required:
# Need to increase initial energy to complete this task.
minimum_energy_needed += (required - current_energy)
current_energy = required
current_energy -= cost
# Ensures we start with enough energy to never go negative.
if current_energy < 0:
minimum_energy_needed -= current_energy
return minimum_energy_needed
Case | How to Handle |
---|---|
Empty tasks array | Return 0 as no energy is needed to complete no tasks. |
Null tasks array | Throw an IllegalArgumentException to indicate invalid input. |
Tasks with extremely large values leading to integer overflow | Use long data type to store energy and task values to prevent overflow. |
Tasks array with a single task [cost, required] | Return the required energy for that single task, Math.max(cost, required). |
Tasks where cost is always greater than required energy. | The minimum initial energy is equal to the maximum cost value in the tasks array |
Tasks where required energy is always greater than cost. | The minimum initial energy needed is the sum of energy differences (required - cost) plus the largest cost. |
Tasks are sorted in increasing order of 'required' energy | The standard algorithm should still work correctly, iterating through the sorted tasks adjusting current energy. |
Tasks array contains negative numbers for cost or required energy. | Throw an IllegalArgumentException as cost and required energy are expected to be non-negative. |