C++ Greedy Algorithm with Custom Sorting
Intuition\nThe key insight is to prioritize tasks that have a larger gap between their minimum required effort and actual cost. By tackling these "high-demand" 
# Intuition\nThe key insight is to prioritize tasks that have a larger gap between their minimum required effort and actual cost. By tackling these "high-demand" tasks first, we can minimize the initial energy required.\n\n# Approach\n1. Sort tasks: Arrange tasks in descending order based on the difference between minimum required effort and actual cost.\n2. Iterate and accumulate:\n   - For each task, check if current energy is sufficient.\n   - If not, increase the total energy required.\n   - Update current energy after each task.\n\n-  The algorithm ensures we always have just enough energy to start each task.\n\n- By processing tasks with larger differences first, we minimize the chance of unnecessarily increasing our initial energy.\n\n- This greedy approach leads to the optimal solution for the minimum initial energy required.\n\n- The sorting is crucial and must be in decreasing order of the difference (minimum required - actual cost). Sorting in increasing order would lead to an incorrect result.\n\n- For tasks with the same difference, their relative order doesn\'t affect the final result.\n\n- The `result` variable represents the total minimum initial energy needed, while `current` keeps track of the energy level after each task.\n\n# Complexity\n- Time complexity: $$O(n log n)$$\nThe sorting step dominates the time complexity, where n is the number of tasks.\n\n- Space complexity: $$O(1)$$ or $$O(log n)$$\n$$O(1)$$ additional space is used for variables. The space complexity of the sorting algorithm (typically $$O(log n)$$ for most built-in sorts) may need to be considered depending on the implementation.\n\n# Code\n```\nclass Solution {\npublic:\n    int minimumEffort(vector<vector<int>>& tasks) {\n        auto comparator = [](const vector<int>& a, const vector<int>& b) {\n            int diffA = a[1] - a[0];\n            int diffB = b[1] - b[0];\n            return diffA > diffB;\n        };\n        sort(tasks.begin(), tasks.end(), comparator);\n        int result = 0;\n        int current = 0;\n        \n        for (const auto& task : tasks) {\n            if (current < task[1]) {\n                result += task[1] - current;\n                current = task[1];\n            }\n            current -= task[0];\n        }\n        \n        return result;\n    }\n};\n```
['C++']