Drill

ProblemsC++ › games

Find the longest winning streak

mediumgamesArraysC++

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

C++ needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

int winStreak(std::vector<bool> results) {
    
}

Worked examples

CallResult
winStreak(std::vector<bool>{true, true, false, true})2
winStreak(std::vector<bool>{false, false, false})0
winStreak(std::vector<bool>{true, true, true})3
winStreak(std::vector<bool>{})0

Hint

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

Reference solution in C++
int winStreak(std::vector<bool> results) {
    int best = 0, cur = 0;
    for (bool r : results) {
        if (r) { cur++; if (cur > best) best = cur; }
        else cur = 0;
    }
    return best;
}

The same problem in another language

More games problems in C++