Introduction
LeetCode 1665 – Minimum Initial Energy to Finish Tasks is an important greedy algorithm problem frequently asked in coding interviews.
The problem looks difficult initially because tasks can be completed in any order. The real challenge is finding the best order that minimizes the starting energy required.
This problem teaches:
- Greedy strategy
- Custom sorting
- Optimization thinking
- Simulation techniques
- Interview-level problem solving
Problem Link
🔗 https://leetcode.com/problems/minimum-initial-energy-to-finish-tasks/
Problem Statement
You are given an array:
Where:
actuali= energy spent after completing the taskminimumi= minimum energy required to start the task
You can complete tasks in any order.
Return the minimum initial energy required to finish all tasks.
Example
Input
Output
Understanding the Problem
Suppose a task is:
This means:
- You need at least
8energy to begin. - After completing it, your energy decreases by
4.
If your current energy is:
After finishing:
Brute Force Approach
Intuition
Try every possible order of tasks and calculate the minimum starting energy needed.
Then return the smallest answer.
Why Brute Force Fails
If there are N tasks:
For large constraints up to 10^5, brute force becomes impossible.
Brute Force Complexity
Time Complexity
Space Complexity
Greedy Intuition
For every task:
The value:
represents how restrictive the task is.
Tasks with larger differences require high starting energy and should be completed earlier.
Key Greedy Observation
We should sort tasks in descending order of:
This minimizes the extra starting energy required later.
Why This Greedy Works
Suppose we have:
Differences:
Task A is more restrictive.
If we delay task A, we may lose too much energy before attempting it.
So we perform tasks with larger (minimum - actual) first.
Optimal Greedy Approach
Steps
1. Sort Tasks
Sort tasks by:
2. Maintain Current Energy
curr= current available energytotal= minimum initial energy required
3. Add Energy When Needed
If:
Add extra energy.
4. Complete the Task
Reduce current energy by actual energy spent.
Java Greedy Solution
Dry Run
Input
Step 1: Sort Tasks
Differences:
| Task | Difference |
| [1,2] | 1 |
| [2,4] | 2 |
| [4,8] | 4 |
Sorted Order:
Step 2: Process Tasks
Task [4,8]
Need energy:
Current energy:
Add:
After completion:
Task [2,4]
Already have enough energy.
After completion:
Task [1,2]
After completion:
Final Answer
Time Complexity Analysis
Time Complexity
Sorting dominates the complexity.
Space Complexity
Ignoring sorting space.
Interview Explanation
In interviews, explain:
Tasks with larger (minimum - actual) are more restrictive because they require high starting energy. Performing them earlier prevents us from needing larger initial energy later.This demonstrates strong greedy reasoning.
Common Mistakes
1. Sorting by Minimum Only
Incorrect because actual energy consumption also matters.
2. Sorting by Actual Only
Also incorrect.
The important factor is:
3. Forgetting to Increase Current Energy
Always check:
before performing the task.
FAQs
Q1. Why sort by (minimum - actual)?
Because it measures how restrictive a task is.
Q2. Is this Dynamic Programming?
No.
This is a Greedy + Sorting problem.
Q3. Why is this problem considered hard?
The greedy observation is difficult to identify.
The implementation itself is straightforward.
Conclusion
LeetCode 1665 is an excellent greedy problem for mastering:
- Custom sorting
- Greedy intuition
- Task scheduling
- Optimization techniques
The key idea is sorting tasks by:
in descending order.
Once this intuition clicks, many advanced greedy interview problems become easier to solve.





