Drill

ProblemsGo › data

The highest so far

easydataArraysPrefix sumsGo

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

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.

Solve it in Python →

Where you start

func runningMax(values []int) []int {
	
}

Worked examples

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

The same problem in another language

More data problems in Go