DP greedy sort
IntuitionWe want do to the tasks with the least difference between the start and actual cost last.Approach
Sort the array by minimum difference between start an
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
We want do to the tasks with the least difference between the start and actual cost last. 
# Approach
<!-- Describe your approach to solving the problem. -->
1. Sort the array by minimum difference between start and actual cost. 
2. Determine the minimum cost to start the first task (tasks[0][1])
3. Build this up by using this to find the minimum cost for the next task.
4. Return the minimum cost to start the end task.

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
$O(n log n)$ 
1. itterate over the array O(n)
2. sorting O(n log n)
3. itterate over the array again O(n)


- Space complexity:
$O(n)$
a constant amount of data is being stored for each element O(n)
# Code
```python3 []
class Solution:
    def minimumEffort(self, tasks: List[List[int]]) -> int:
        # Creates a 3rd element in each inner array which represents the difference between the inital energy and cost
        for item in tasks:
            item.append(item[1] - item[0])
        # Sorts by difference
        tasks.sort(key=self.sort_key)

        # Making a prefix array of minimum cost to start from the current point
        tasks[0].append(tasks[0][1])
        for i in range(1, len(tasks)):
            # If the start cost of the current task > cumulitive start cost for previous tasks
            if tasks[i][1] > tasks[i-1][3]:
                tasks[i].append(tasks[i][1])
                # If the start cost for this minus the cost to complete < min start cost for previous 
                if tasks[i][1] - tasks[i][0] < tasks[i-1][3]:
                    # Min cost to start current = min cost to start previous + cost to complete this
                    tasks[i][3] = tasks[i-1][3] + tasks[i][0]
            else:
                # The min cost to start current = min cost to start previous + cost to complete current
                tasks[i].append(tasks[i-1][3])
                tasks[i][3] += tasks[i][0]

        return tasks[-1][3]

    def sort_key(self, e):
        return e[2]

```
['Dynamic Programming', 'Greedy', 'Sorting', 'Prefix Sum', 'Python3']