Minimum Initial Energy to Finish Tasks - Problem
Imagine you're a space explorer with limited energy reserves, and you have a series of critical missions to complete before your journey ends. Each mission has two important characteristics:
actual[i]: The energy you'll actually consume during the missionminimum[i]: The minimum energy required to even start the mission
Here's the catch: you can choose the order in which to tackle these missions! Your goal is to find the minimum initial energy needed to complete all missions successfully.
Example: If a mission is [10, 12] and you have 11 energy units, you cannot start this mission. But if you have 13 energy units, you can complete it and will have 13 - 10 = 3 energy remaining.
The strategic challenge lies in determining the optimal order to minimize your starting energy requirements.
Input & Output
example_1.py โ Basic Case
$
Input:
tasks = [[1,2],[2,4],[4,8]]
โบ
Output:
8
๐ก Note:
Starting with 8 energy: do task 3 (8-4=4 left), then task 2 (4-2=2 left), then task 1 (2-1=1 left). All tasks completed successfully.
example_2.py โ Equal Requirements
$
Input:
tasks = [[1,3],[2,3],[3,3]]
โบ
Output:
6
๐ก Note:
All tasks have minimum=3. Start with 6: do task 3 (6-3=3), task 2 (3-2=1), but can't do task 1 (need 3, have 1). So we need more energy initially.
example_3.py โ Single Task
$
Input:
tasks = [[1,1]]
โบ
Output:
1
๐ก Note:
Only one task requiring minimum 1 energy and consuming 1 energy. Need exactly 1 energy to start.
Constraints
- 1 โค tasks.length โค 105
- 1 โค actuali โค minimumi โค 104
- actuali โค minimumi (you always need at least as much energy as you consume)
Visualization
Tap to expand
Understanding the Visualization
1
Mission Analysis
Each space mission requires minimum energy to start but only consumes a portion
2
Strategic Ordering
Missions with higher energy buffers should be prioritized when energy reserves are full
3
Resource Planning
Work backwards from final mission to determine minimum starting energy reserves needed
Key Takeaway
๐ฏ Key Insight: By tackling missions with higher energy buffer requirements first (when our reserves are fullest), we minimize the total energy needed to complete all missions successfully.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code