python3 with explaination
Sorted tasks by the different between minimum required energy and energy cost, which is x[1]-x[0].\nKeep merging two tasks into a single task, until we only hav
Sorted tasks by the different between minimum required energy and energy cost, which is *x[1]-x[0]*.\nKeep merging two tasks into a single task, until we only have one task.\n\nThe cost of the combined task equals to the sum of the energy cost of two tasks.\nFor minimum required energy, lets assume we start with task1 then task2, we need to\n* energy>task1[1] \n* spend task1[0] and then have remaining energy>task2[1] satisfied. Thus for this order (t1 -> t2) we have:\n**minimum energy= max(task1[1], task1[0]+ task2[1])**, we have to meet both requirements in order to finish these two tasks in this order.\n\nHowever we can also finish the task pairs in another order, which is task2 -> task1, where the minimum energy is\n**minimum energy= max(task2[1], task2[0]+ task1[1])**\n\nTherefore for a certain task pair, the minimum energy requirement is the smaller one.\n\n\n```\ndef minimumEffort(tasks):\n    def merg(t1,t2):\n        return t1[0]+t2[0],min(max(t1[1]+t2[0],t2[1]),max(t1[0]+t2[1],t1[1]))\n    ans=sorted(tasks,key=lambda x:x[1]-x[0])[::-1]\n    return functools.reduce(merg,ans)[1]\n```\n\n\n
[]