Drill

ProblemsPython › games

Find the longest winning streak

mediumgamesPython

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

win_streak(results: list<bool>) → int

Solve it in the editor →

Where you start

def win_streak(results: list[bool]) -> int:
    

Worked examples

CallResult
win_streak([True, True, False, True])2
win_streak([False, False, False])0
win_streak([True, True, True])3
win_streak([])0

Hint

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

Reference solution in Python
def win_streak(results: list[bool]) -> int:
    best, cur = 0, 0
    for r in results:
        if r:
            cur += 1
            if cur > best:
                best = cur
        else:
            cur = 0
    return best

The same problem in another language

More games problems in Python