Created
March 25, 2025 09:06
-
-
Save lndgalante/6dfc2ad5836398e729852d13797acb1e to your computer and use it in GitHub Desktop.
Find longest streak
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
| 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