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 mission
  • minimum[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
Home BaseMission 1Energy: [1,3]Mission 2Energy: [4,8]Mission 3Energy: [2,4]Space ExplorerOptimal Mission Sequence: High Buffer First
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.
Asked in
Google 42 Amazon 38 Meta 25 Microsoft 31
52.5K Views
Medium Frequency
~25 min Avg. Time
1.8K 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