The highest so far
A stock chart underlays the running peak: after each point, how far has the price climbed at most.
- Each output entry is the largest value seen from the start up to that point.
- The result is the same length as the input.
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.
Where you start
std::vector<int> runningMax(std::vector<int> values) {
}
Worked examples
| Call | Result |
|---|---|
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;
}