6
0
Write a function that takes two arguments: a number k
and a list l
, and that returns another list r
such that r[i] = min(l[i-k+1:i])
.
Examples
- Input k=2, l=1,2,3,4,5,6
- Ouput r=1,1,2,3,4,5
- Input k=3, l=-1,9,7,-9,8,3,5,0,12,1,2,3,-1,-2,-3
- Ouput r=-1,-1,-1,-9,-9,-9,3,0,0,0,1,1,-1,-2,-3
Constraints: O(len(l)) time complexity. Shortest code wins.
3I don't really understand the formula after sucht that..., can you explain it further to me? – FUZxxl – 2011-02-06T14:21:27.530
@FUZxxl: r[i] is minimum of the last k elements in l ending at i. – Alexandru – 2011-02-06T14:43:53.117
This task is probably as complex as a sort, so O(len(l)) can be obtained – edc65 – 2017-01-26T11:23:29.573
Is the function allowed to just modify the original array, or does it need to return a new one? – 12Me21 – 2017-01-26T14:59:57.113
Please use the
code-golf
tag when asking for shortest code. – marcog – 2011-02-05T08:34:41.403Why the complexity contraint if the goal is shortest code? – hallvabo – 2011-02-05T13:42:21.167