Minimum Initial Energy to Finish Tasks Runtime 100% and Memory 90 %
Intuition\nThe key insight is that we want to minimize the initial energy required to complete all tasks. Tasks with a high minimum requirement but low actual e
# Intuition\nThe key insight is that we want to minimize the initial energy required to complete all tasks. Tasks with a high minimum requirement but low actual energy consumption should be done first, as they have the biggest impact on our initial energy needs.\n\n# Approach\n1. Sort the tasks based on the difference between minimum required energy and actual energy consumption (minimum - actual) in descending order\n2. Keep track of two variables:\n   - `currentEnergy`: represents our current energy level\n   - `initialEnergy`: represents the minimum initial energy needed\n3. For each task:\n   - If our current energy is less than the minimum required, we need to increase our initial energy\n   - After completing each task, reduce our current energy by the actual energy consumed\n\n# Complexity\n- Time complexity: **O(n log n)**\n  - The sorting operation takes O(n log n)\n  - The loop through tasks takes O(n)\n  - Overall complexity is dominated by the sort: O(n log n)\n\n- Space complexity: **O(1)**\n  - We only use a constant amount of extra space regardless of input size\n  - The sorting is typically done in-place for arrays\n\n# Code Explanation\n```csharp\n// Sort tasks by (minimum - actual) difference in descending order\n// This ensures we handle tasks with highest energy requirements first\nArray.Sort(tasks, (a, b) => (b[1] - b[0]) - (a[1] - a[0]));\n\nforeach (var task in tasks) {\n    int actual = task[0];    // Energy consumed by the task\n    int minimum = task[1];   // Minimum energy required to start\n    \n    // If current energy is less than minimum required\n    // We need to increase our initial energy\n    if (currentEnergy < minimum) {\n        initialEnergy += minimum - currentEnergy;\n        currentEnergy = minimum;\n    }\n    \n    // After task completion, reduce energy by actual amount used\n    currentEnergy -= actual;\n}\n```
['C#']