[easy intuition] [step-by-step explanation] sorting solution
Intuitionsome terminology : process a task = finish a task = finish an elementNow, let think in the direction if we are not given with the term minimumi​.
Then,
# Intuition
some terminology : process a task = finish a task = finish an element


Now, let think in the direction if we are not given with the term $$\text{minimum}_i$$. 
Then, our answer would always be sum of all the $$\operatorname{actual}_i$$.
So, lets start with this answer only, that is, initial energy = sum of all the $$\operatorname{actual}_i$$.
Now, $$\text{minimum}_i$$ wants to enforce a energy threshold over each $$i$$. That is, if I have current energy = 10 (lets say), and element is like [8, 13]. Then, i can finish this element as 10 >= 8, but since minimum threshold = 13 exists, which means i have to increase my energy by 3 (= 13-10) to finish this element.

So, What we can do is initiate our ans with initial energy = sum of all the $$\operatorname{actual}_i$$ and then traverse over each element and whenever we have low in energy (like in above case, current energy = 10 (lets say), and element is like [8, 13]), we will increase our energy by that much amount (here, 3 (= 13-10)). In code, we are storing this in  ```toAdd``` variable.


Now, our main work remaining is how to traverse the array, so that, our ```toAdd``` tem is minimum so that our answer is optimal.

Now, Consider tasks = ```[[1,3],[2,4],[10,11],[10,12],[8,9]]```

Lets, sort this array = ```[[1,3],[2,4],[8,9],[10,11],[10,12]]```

Now, first thing comes in mind is to finish the elements with low $$\text{minimum}_i$$.

another thing comes in mind is to finish the elements with high $$\text{minimum}_i$$. 

lets do both and compare our approaches.

1. finish the elements with low $$\text{minimum}_i$$.
```initial answer = 1 + 2 + 8 + 10 + 10 = 31```
```toAdd = 0 ```
array = ```[[1,3],[2,4],[8,9],[10,11],[10,12]]```
i=0, 31-1 = 30
i=1, 30-2 = 28
i=2, 28-8 = 20
i=3, 20-10 = 10
i=4, now , 10 <= 12, need to increase energy by 2, hence toAdd += 2, and energy += 2, hence, finally,  12 - 10 = 2.

Hence, our final answer would be initial_energy + toAdd = 31+2 = 33.
But our optimal answer is 32, that is, toAdd  = 1;

2. finish the elements with high $$\text{minimum}_i$$.
```initial answer = 1 + 2 + 8 + 10 + 10 = 31```
```toAdd = 0 ```
array = ```[[1,3],[2,4],[8,9],[10,11],[10,12]]```
i=4, 31-10 = 21
i=3, 21-10 = 11
i=2, 11-8 = 3
i=1, now , 3<=4, need to increase energy by 1, hence toAdd += 1, and energy += 1, hence, finally,  4 - 2 = 2.
i=0, now , 2<=3, need to increase energy by 1, hence toAdd += 1, and energy += 1, hence, finally,  3-1 = 2

Hence, our final answer would be initial_energy + toAdd = 31+2 = 33.
But our optimal answer is 32, that is, toAdd  = 1;

Now, both the above approaches are not giving optimal answer, that is regular sorting won't work. We need to think in an optimal way. 

Now, lets think in way that [$\text{minimum}_i$, $\text{actual}_i$]. Now,
$\text{actual}_i$ is actual energy needed, while  $\text{minimum}_i$ is minimum energy needed. We can see that it is always, 
$\text{minimum}_i$ >= $\text{actual}_i$,
 other wise , array of tasks in not valid. Suppose  $\text{actual}_i$ = 7 and $\text{minimum}_i$ = 4, which means minimum energy needed is 4 while energy needed to finish the task is 7. So, if you have current energy = 5, you can finish this task, and hence final energy =5 - 7 = -2, which is not possible.

Now, 

![image.png](https://assets.leetcode.com/users/images/3ef11643-fac7-41fb-8388-0f2e9fbc4b6a_1738383301.30941.png)

think the difference between $\text{minimum}_i$ and $\text{actual}_i$. Now, in which order you want to traverse the array ? 

Now, suppose you are at the current energy level ```x```, and $\text{minimum}_i$ is significantly larger than $\text{actual}_i$. In this case, you should always prioritize finishing this element first. Why? Because your current energy level is already higher than $\text{minimum}_i$, allowing you to easily cross this threshold. However, since the actual energy required is much lower, your energy level will decrease only slightly. This minimizes energy consumption, increasing the chances that your remaining energy will be higher for the next element, allowing you to cross further thresholds more effectively.

So, We want to process elements with the highest difference between their actual and minimum energy needed, and so on. But if the difference between some elements are same, then what ? then, we will process element with lower thresholds first, so that, we can finish some elements without increasing our ```toAdd``` variable.



# Approach
<!-- Describe your approach to solving the problem. -->
sort the array of tasks based on the difference between actual and minimum energy needs. If difference is same, we want to process the lowest minimum_i first. In the code, we are doing -tasks[i][1] only for sorting purposes

# Complexity
- Time complexity: ```O(nlogn)```

- Space complexity: ```O(n)```

# Code
```cpp []
class Solution {
public:
    int minimumEffort(vector<vector<int>>& tasks) {
        int n = tasks.size();
        vector<vector<int>> arr(n);
        int startEnergy = 0, toAdd = 0;
        for (int i = 0; i < n; i++) {
            startEnergy += tasks[i][0];
            int diff = tasks[i][1] - tasks[i][0];
            arr[i] = {diff, -tasks[i][1],
                      tasks[i][0]}; /// difference, min, actual.
                                    ////.      0      , 1,   2
        }
        int startEnergyCopy = startEnergy;
        sort(arr.begin(), arr.end());
        for (int i = n - 1; i >= 0; i--) {
            if (startEnergy >= -arr[i][1]) {
                startEnergy -= arr[i][2];
            } else {
                int temp = -arr[i][1] - startEnergy;
                toAdd += temp;
                startEnergy += temp;
                startEnergy -= arr[i][2];
            }
        }
        return startEnergyCopy + toAdd;
    }
};
```
['Greedy', 'Sorting', 'C++']