CPP Sorting + Binary Search
Complexity

Time complexity:
O(NlogM)


Space complexity:
O(1)

Code


# Complexity
- Time complexity:
O(NlogM)

- Space complexity:
O(1)

# Code
```cpp []
class Solution {
public:
    bool canComplete(long long currentEnergy, vector<vector<int>>& tasks) {
        for (auto t : tasks) {
            if (currentEnergy >= t[1]) {
                currentEnergy -= t[0];
            } else {
                return false;
            }
        }

        return true;
    }

    int minimumEffort(vector<vector<int>>& tasks) {
        // 1. sort by (minimum - actual) because a greater (minimum - actual)
        // value requires a high starting energy but does not consume much
        // actual energy hence we might have enough energy to start other tasks
        // as well.

        // 2. binary search on len(tasks) ..... sum(tasks[1])

        sort(tasks.begin(), tasks.end(), [](vector<int>& t1, vector<int>& t2) {
            return (t1[1] - t1[0]) > (t2[1] - t2[0]);
        });

        long long low = tasks.size();
        long long high = 1e9;

        while (low <= high) {
            long long mid = low + (high - low) / 2;
            if (canComplete(mid, tasks)) {
                high = mid - 1;
            } else {
                low = mid + 1;
            }
        }

        return low;
    }
};
```
['Array', 'Greedy', 'Sorting', 'C++']