Drill

ProblemsJava › data

The highest so far

easydataArraysPrefix sumsJava

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

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.

Solve it in Python →

Where you start

List<Integer> runningMax(List<Integer> values) {
    
}

Worked examples

CallResult
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;
}

The same problem in another language

More data problems in Java