Clean Java Code O(n log n)
\n\n    public int minimumEffort(int[][] tasks) {\n        List energyList = getEnergyList(tasks);\n        return getMinimumRequiredEnergy(energyList);\n    }\
\n\n    public int minimumEffort(int[][] tasks) {\n        List<Energy> energyList = getEnergyList(tasks);\n        return getMinimumRequiredEnergy(energyList);\n    }\n\n    private int getMinimumRequiredEnergy(List<Energy> energyList) {\n        int requiredEnergy = 0;\n        int remainingEnergy = 0;\n        for (Energy energy : energyList) {\n            if (energy.minimum > remainingEnergy) {\n                int d = energy.minimum - remainingEnergy;\n                requiredEnergy += d;\n                remainingEnergy += d;\n            }\n            remainingEnergy -= energy.actual;\n        }\n        return requiredEnergy;\n    }\n\n    private List<Energy> getEnergyList(int[][] tasks) {\n        List<Energy> energyList = new ArrayList<>();\n        for (int[] task : tasks) {\n            Energy energy = new Energy(task[0], task[1]);\n            energyList.add(energy);\n        }\n        Collections.sort(energyList);\n        return energyList;\n    }\n\n    class Energy implements Comparable<Energy>{\n        int actual;\n        int minimum;\n        int diff;\n\n        public Energy(int actual, int minimum){\n            this.actual = actual;\n            this.minimum = minimum;\n            diff = minimum - actual;\n        }\n\n        public int compareTo(Energy compareEnergy){\n            int diff = compareEnergy.diff;\n            int diffOfDiff = diff - this.diff;\n            if(diffOfDiff == 0) {\n                return this.minimum - compareEnergy.minimum;\n            }\n            return diff - this.diff;\n        }\n    }
[]