Magicsheet logo

Minimum Initial Energy to Finish Tasks

Hard
100%
Updated 6/1/2025

Minimum Initial Energy to Finish Tasks

1. What is this problem about?

The "Minimum Initial Energy to Finish Tasks" interview question is a sophisticated scheduling problem that combines element processing with resource constraints. You are given a list of tasks, where each task is defined by two values: the actual energy required to complete the task and the minimum energy required to start the task.

The rule is that you can only begin a task if your current energy is greater than or equal to its minimum requirement. Once you finish the task, your energy decreases by the actual amount. The goal is to determine the minimum initial energy you need to have at the very beginning to complete all given tasks in any order you choose.

2. Why is this asked in interviews?

Companies like Microsoft and Google use this problem to test a candidate's ability to apply Greedy Algorithms and Sorting to optimization problems. It's not immediately obvious in what order the tasks should be performed to minimize the starting energy.

Key skills being evaluated:

  • Mathematical Observation: Can the candidate derive the optimal sorting criteria?
  • Resource Management Logic: Understanding that "buffer" energy (the difference between minimum and actual) is the key to minimizing the total.
  • Efficiency: Implementing the solution in O(nlogn)O(n \log n) time.

3. Algorithmic pattern used

The core pattern is Greedy with Custom Sorting. The "trick" to this problem is realizing that tasks with a large difference between minimum and actual energy should be tackled first. This difference (minimum - actual) represents the amount of energy that must be "kept in reserve" but isn't actually consumed.

By sorting tasks in descending order of this difference (minimum - actual), we ensure that we use our high initial energy to satisfy the most demanding "starting" requirements while we still have enough "buffer" energy available.

4. Example explanation

Suppose we have two tasks:

  • Task A: actual = 4, minimum = 10 (Difference = 6)
  • Task B: actual = 8, minimum = 12 (Difference = 4)

Order 1: A then B

  1. To start A, we need 10. Energy becomes 104=610 - 4 = 6.
  2. To start B, we need 12. Our current energy is 6. We need 6 more. Total starting = 10+6=1610 + 6 = 16.

Order 2: B then A (Correct Greedy Order, diff 4 is less than 6) Wait, let's check: Task A diff is 6, Task B diff is 4. Sorting by difference descending means A then B. Let's check if 16 works. Start with 16.

  • Task A: 161016 \ge 10 (OK). Energy becomes 164=1216 - 4 = 12.
  • Task B: 121212 \ge 12 (OK). Energy becomes 128=412 - 8 = 4. Total starting energy: 16.

What if we started with B?

  • Task B: 161216 \ge 12 (OK). Energy becomes 168=816 - 8 = 8.
  • Task A: 8<108 < 10 (FAIL). To finish A, we'd need to start with 18. So 16 is indeed the minimum.

5. Common mistakes candidates make

  • Sorting by Minimum only: Thinking that tasks requiring the most starting energy should always go first.
  • Sorting by Actual only: Thinking that tasks that consume the most energy should go first.
  • Simulating without Sorting: Trying to use a brute-force permutation approach, which is O(n!)O(n!) and will time out.
  • Off-by-one in Calculation: Forgetting that once a task is finished, the energy is consumed, which affects the ability to start the next one.

6. Interview preparation tip

Whenever you encounter a problem where you need to "pick an order" to minimize or maximize something, think "Greedy with Sorting." Try to find a formula for comparing two items. Ask yourself: "If I have Task 1 and Task 2, under what condition is 121 \rightarrow 2 better than 212 \rightarrow 1?"

Similar Questions