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
public List<int> RunningMax(List<int> values) {
}
Worked examples
| Call | Result |
|---|---|
RunningMax(new List<int> { 3, 1, 2 }) | new List<int> { 3, 3, 3 } |
RunningMax(new List<int> { 1, 2, 3 }) | new List<int> { 1, 2, 3 } |
RunningMax(new List<int> { 5, -2, 7 }) | new List<int> { 5, 5, 7 } |
RunningMax(new List<int> { 2, 1, 3, 2 }) | new List<int> { 2, 2, 3, 3 } |
Hint
Keep one running best and stamp it into the result after considering each value.
Reference solution in C#
public List<int> RunningMax(List<int> values) {
var result = new List<int>();
int best = 0;
bool started = false;
foreach (var v in values) {
if (!started || v > best) { best = v; started = true; }
result.Add(best);
}
return result;
}