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>
Go 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
func runningMax(values []int) []int {
}
Worked examples
| Call | Result |
|---|---|
runningMax([]int{3, 1, 2}) | []int{3, 3, 3} |
runningMax([]int{1, 2, 3}) | []int{1, 2, 3} |
runningMax([]int{5, -2, 7}) | []int{5, 5, 7} |
runningMax([]int{2, 1, 3, 2}) | []int{2, 2, 3, 3} |
Hint
Keep one running best and stamp it into the result after considering each value.
Reference solution in Go
func runningMax(values []int) []int {
result := []int{}
best := 0
started := false
for _, v := range values {
if !started || v > best {
best = v
started = true
}
result = append(result, best)
}
return result
}