Drill

ProblemsTypeScript › games

Find the longest winning streak

mediumgamesTypeScript

A sequence of game results is recorded as wins and losses. Find the length of the longest unbroken streak of wins.

winStreak(results: list<bool>) → int

Solve it in the editor →

Where you start

function winStreak(results: boolean[]): number {
  
}

Worked examples

CallResult
winStreak([true,true,false,true])2
winStreak([false,false,false])0
winStreak([true,true,true])3
winStreak([])0

Hint

Keep a running counter that resets on every false, and track the best so far.

Reference solution in TypeScript
function winStreak(results: boolean[]): number {
  let best = 0;
  let cur = 0;
  for (const r of results) {
    if (r) { cur++; if (cur > best) best = cur; }
    else cur = 0;
  }
  return best;
}

The same problem in another language

More games problems in TypeScript