Drill

ProblemsC++ › data

The highest so far

easydataArraysPrefix sumsC++

A stock chart underlays the running peak: after each point, how far has the price climbed at most.

runningMax(values: list<int>) → list<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

std::vector<int> runningMax(std::vector<int> values) {
    
}

Worked examples

CallResult
runningMax(std::vector<int>{3, 1, 2})std::vector<int>{3, 3, 3}
runningMax(std::vector<int>{1, 2, 3})std::vector<int>{1, 2, 3}
runningMax(std::vector<int>{5, -2, 7})std::vector<int>{5, 5, 7}
runningMax(std::vector<int>{2, 1, 3, 2})std::vector<int>{2, 2, 3, 3}

Hint

Keep one running best and stamp it into the result after considering each value.

Reference solution in C++
std::vector<int> runningMax(std::vector<int> values) {
    std::vector<int> result;
    int best = 0;
    bool started = false;
    for (int v : values) {
        if (!started || v > best) { best = v; started = true; }
        result.push_back(best);
    }
    return result;
}

The same problem in another language

More data problems in C++