[Java] Straightforward from the question
First, we can think about stratgy to have minimum energy to do all the tasks.\n\nThe first idea come to mind is working on a task with highest minimum value. Ho
First, we can think about stratgy to have minimum energy to do all the tasks.\n\nThe first idea come to mind is working on a task with highest minimum value. However, we can easily find the case where this does not work.\nFor example, with [1, 10] and [1, 2], we should work on [1, 2] first to finish them with 11 energy.\n\nThen, should we work on task with lowest minimum value? Then, there is a case where it does not work. [1, 1] and [1, 4].\n\nYou found the pattern here. Maybe we should work first on task with largest gap between actual energy and minimum energy. \nBy working first on a task having the largest gap between actual energey and minimum energy, we can use actual energy of all remaining tasks to meet the minimum energe of the task.\n\n```\nclass Solution {\n\tpublic int minimumEffort(int[][] tasks) {\n\t\tint actual = 0;\n\t\t// Sort task in decreasing order of gap between actual energy and minimum energy\n\t\tArrays.sort(tasks, (a, b) -> ((b[1] - b[0]) - (a[1] - a[0])));\n\t\t\n\t\t// Sum actual energy to work on all tasks.\n\t\tfor (int[] task : tasks) {\n\t\t\tactual += task[0];\n\t\t}\n        \n\t\t// We start from this energy\n\t\tint min = actual;\n\t\tfor (int[] task : tasks) {\n\t\t    // If remaining energy is lower than minimum energy, we increase that amount.\n\t\t\tif (actual < task[1]) {\n\t\t\t\tmin += task[1] - actual;\n\t\t\t\tactual = task[1];\n\t\t\t}    \n\t\t\t// Remove energy we used.\n\t\t\tactual -= task[0];\n\t\t}\n        \n\t\treturn min;\n\t}\n}\n```\n\nNow if you see the code above, we are subtracting energy while iterating tasks. \nWhat if we do it in reverse way? \n\nIn this new approach, we need to sort the task in reverse way.\nWe don\'t need to sum the actual energy to start with. Instead, we add actual energy while iterating tasks.\nIf energy we have is not enough for minimum energy requred for a task, we set actual energy to the minimum energy.\n```\nclass Solution {\n    public int minimumEffort(int[][] tasks) {\n        int actual = 0;\n        Arrays.sort(tasks, (a, b) -> ((a[1] - a[0]) - (b[1] - b[0])));\n        for (int[] task : tasks) {\n            actual += task[0];\n            if (actual < task[1]) {\n                actual = task[1];\n            }    \n        }\n                    \n        return actual;\n    }\n}\n```
[]