Simple math | Easy to Understand | Python
IntuitionBinary search is the first intutionApproachlet us take the general example of two tasks
task1 = [actual1, minimum1] and task2 = [actual2, minimum2]let
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
Binary search is the first intution

# Approach
<!-- Describe your approach to solving the problem. -->
let us take the general example of two tasks
task1 = [actual1, minimum1] and task2 = [actual2, minimum2]

let us first consider the case where we do task1 first and the minimum time required to complete both tasks is x

x should be greater than minimum1 and after subtracting actual1 it should be greater than minimum2, this gives us two equations

x >= minimum1 --------------------------------------->(1)
x-actual1 >= minimum2 -> x >= minimum2+actual1 --->(2)

using equation 1 and 2 we can clearly see that
x >= max(minimum1,minimum2+actual1)
        
similarily if we take the second case where we do task2 first and the minimum time required to complete both tasks is y

y >= max(minimum2,minimum1+actual2)

Now the question boils down to a simpler form -> find which is bigger x or y, and this can be done using a custom comparator

After sorting the tasks using this compartor the problem becomes a simple binary search problem

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```python3 []
from functools import cmp_to_key
class Solution:
    def minimumEffort(self, tasks: List[List[int]]) -> int:
        def func(a,b):
            if max(a[0],b[0]+a[1]) < max(b[0],a[0]+b[1]):
                return 1
            elif max(a[0],b[0]+a[1]) > max(b[0],a[0]+b[1]):
                return -1
            return 0

        def isPossible(energy):
            for i in range(n):
                if energy < tasks[i][1]:
                    return False
                energy -= tasks[i][0]
            return True

        n = len(tasks)
        tasks.sort(key = cmp_to_key(func))

        left, right = tasks[-1][-1], sum([tasks[i][1] for i in range(n)])

        while left <= right:
            mid = (left+right)//2
            if isPossible(mid):
                ans = mid
                right = mid-1
            else:
                left = mid+1

        return ans
```
['Python3']