[C++] Greedy Solution Simple and Clear Explanation
Let\'s break the problem into two parts:\n1. Ordering the tasks\n2. Calculating the tasks\n\n1. Ordering the tasks\n\nIntuition:\nConsider a particular task, wh
Let\'s break the problem into two parts:\n1. Ordering the tasks\n2. Calculating the tasks\n\n**1. Ordering the tasks**\n\n**Intuition:**\nConsider a particular task, which has `A` actual energy required and `M` minimum energy required. So, in order to complete this task we need to provide atleast `M` energy. But `M-A` will get wasted. Since our prime objective is to minimise this wastage, we will first solve those tasks which encurrs minimum wastage, and so on. Basically we need to sort the tasks in ascending order of `M-A`.  Now our ordering part is complete. \n\nThe code for this part would be \n\n>N.B: There can be more optimised way to achieve this, but in contest this was the first method that came to my mind. \n\n```cpp\n\tvector<pair<int, int>> vp;\n\tfor(int i=0; i<n; i++){\n\t\tvp.push_back({tasks[i][1]-tasks[i][0], i});\n\t}\n\tsort(vp.begin(), vp.end());\n```\n\n**2. Calculating the energy**\n\nCreate a variable called `energy=0` which is the energy required to solve the tasks so far.Now iterate through the ordered tasks. At each task, we will compare the `energy+actual` so far with the `minimum` energy needed for the task:\n\tThere can be two outcome, \n\t\t1. `energy+actual` >`minimum` : This way we will just add the actual to the energy. `energy += actual`. Reason being if we had `energy+actual` for this task it will need `actual` to solve it and we will be left with `energy` which satisfies our assumption\n\t\t2. Else, we will make `energy=minimum`.For obvious reasons, cause otherwise we won\'t be able to complete this task\n```cpp\n\t\tint cost = tasks[(vp[0].second)][1];\n        for(int i=1; i<n; i++){\n            cost += tasks[vp[i].second][0];\n            cost = max(cost, tasks[vp[i].second][1]);\n        }\n        \n        return cost;\n```\n\nPutting everything together:\n\n```cpp\nclass Solution {\npublic:\n    int minimumEffort(vector<vector<int>>& tasks) {\n        int n=tasks.size();\n        vector<pair<int, int>> vp;\n        for(int i=0; i<n; i++){\n            vp.push_back({tasks[i][1]-tasks[i][0], i});\n        }\n        \n        sort(vp.begin(), vp.end());\n        int cost = tasks[(vp[0].second)][1];\n        // cout<<cost<<"\\n";\n        for(int i=1; i<n; i++){\n            // cout<<tasks[vp[i].second][1]<<", ";\n            cost += tasks[vp[i].second][0];\n            cost = max(cost, tasks[vp[i].second][1]);\n        }\n        \n        return cost;\n    }\n};\n```\n\nStatus: **All test case passed**
['Greedy', 'Sorting']