Skip to content

Instantly share code, notes, and snippets.

@tatsuyax25
Created May 12, 2026 16:21
Show Gist options
  • Select an option

  • Save tatsuyax25/cb234c5ee40192ea3d103d471486cba6 to your computer and use it in GitHub Desktop.

Select an option

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,
/**
* @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