Created
May 12, 2026 16:21
-
-
Save tatsuyax25/cb234c5ee40192ea3d103d471486cba6 to your computer and use it in GitHub Desktop.
You are given an array tasks where tasks[i] = [actuali, minimumi]: actuali is the actual amount of energy you spend to finish the ith task.
minimumi is the minimum amount of energy you require to begin the ith task.
For example, if the task is [10,
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @param {number[][]} tasks | |
| * @return {number} | |
| */ | |
| var minimumEffort = function(tasks) { | |
| // Sort by (minimum - actual) descending | |
| tasks.sort((a, b) => (b[1] - b[0]) - (a[1] - a[0])); | |
| let totalActual = 0; // sum of actual costs of tasks done so far | |
| let answer = 0; // minimal initial energy required | |
| for (let [actual, minimum] of tasks) { | |
| // For this task, we need: initial >= minimum + totalActualBefore | |
| answer = Math.max(answer, minimum + totalActual); | |
| totalActual += actual; | |
| } | |
| return answer; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment