Skip to content

Instantly share code, notes, and snippets.

@lndgalante
Created March 25, 2025 09:06
Show Gist options
  • Select an option

  • Save lndgalante/6dfc2ad5836398e729852d13797acb1e to your computer and use it in GitHub Desktop.

Select an option

Save lndgalante/6dfc2ad5836398e729852d13797acb1e to your computer and use it in GitHub Desktop.
Find longest streak
function findLongestStreak(booleans: boolean[], streakGoal: number): number {
const streaks: number[] = [];
let indexStreak = 0;
for (let index = 0; index < booleans.length + 1; index++) {
const boolean = booleans[index];
if (boolean === true) {
streaks[indexStreak] = (streaks[indexStreak] ?? 0) + 1;
}
if (boolean === false) {
indexStreak++;
}
}
const longestStreak = Math.max(...streaks);
return longestStreak >= streakGoal ? longestStreak : 0;
}
console.log(findLongestStreak([true, true, false, true, true, true], 3));
// 3
console.log(findLongestStreak([true, true, true, false, true], 4));
// 0
console.log(findLongestStreak([true, true, true, true], 2));
// 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment