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>
Java 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
List<Integer> runningMax(List<Integer> values) {
}
Worked examples
| Call | Result |
|---|---|
runningMax(Main.<Integer>ls(3, 1, 2)) | Main.<Integer>ls(3, 3, 3) |
runningMax(Main.<Integer>ls(1, 2, 3)) | Main.<Integer>ls(1, 2, 3) |
runningMax(Main.<Integer>ls(5, -2, 7)) | Main.<Integer>ls(5, 5, 7) |
runningMax(Main.<Integer>ls(2, 1, 3, 2)) | Main.<Integer>ls(2, 2, 3, 3) |
Hint
Keep one running best and stamp it into the result after considering each value.
Reference solution in Java
List<Integer> runningMax(List<Integer> values) {
List<Integer> result = new ArrayList<>();
int best = 0;
boolean started = false;
for (int v : values) {
if (!started || v > best) { best = v; started = true; }
result.add(best);
}
return result;
}