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.
running_max(values: list<int>) → list<int>
Where you start
def running_max(values: list[int]) -> list[int]:
Worked examples
| Call | Result |
|---|---|
running_max([3, 1, 2]) | [3, 3, 3] |
running_max([1, 2, 3]) | [1, 2, 3] |
running_max([5, -2, 7]) | [5, 5, 7] |
running_max([2, 1, 3, 2]) | [2, 2, 3, 3] |
Hint
Keep one running best and stamp it into the result after considering each value.
Reference solution in Python
def running_max(values: list[int]) -> list[int]:
best = 0
started = False
result = []
for v in values:
if not started or v > best:
best = v
started = True
result.append(best)
return result