Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save chaosmonster/80b196254e7d367c68d0219f758f48c2 to your computer and use it in GitHub Desktop.

Select an option

Save chaosmonster/80b196254e7d367c68d0219f758f48c2 to your computer and use it in GitHub Desktop.
// written while juggling a baby- not tested and not verified
function maxScoreWithReset(nums) {
let maxScore = 0;
let aboveZero = false;
let reset = false;
for(const num in nums) {
maxScore = maxScore + num;
// only one reset allowed
if (reset) {
continue;
}
// we only reset once we were above zero at least once
if (maxScore > 0 && !aboveZero) {
aboveZero = true;
}
// reset when it's best for us
if (aboveZero && maxScore < 0) {
maxScore = 0;
reset = true;
}
}
return maxScore;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment