Created
January 6, 2026 13:01
-
-
Save wperron/9b117e851829f9db6fb0870120d2f8eb to your computer and use it in GitHub Desktop.
max score with one reset
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
| // 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