Skip to content

Instantly share code, notes, and snippets.

@wperron
Created January 6, 2026 13:01
Show Gist options
  • Select an option

  • Save wperron/9b117e851829f9db6fb0870120d2f8eb to your computer and use it in GitHub Desktop.

Select an option

Save wperron/9b117e851829f9db6fb0870120d2f8eb to your computer and use it in GitHub Desktop.
max score with one reset
// Given an array of numbers, sums the element in order. The sum is reset
// exactly once during this process. The function returns the maximum sum.
function maxScore(arr) {
let sum = 0;
let max = arr[arr.length - 1];
// for exactly one reset, i > 0
// for at most one reset, i >= 0
for (let i = arr.length - 1; i > 0; i--) {
sum += arr[i];
arr[i] = sum;
max = Math.max(max, sum);
}
return Math.max(max, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment