I have this task:
Given an array of integers and a number k, where 1 <= k <= length of the array, compute the maximum values of each subarray of length k. For example, given array = [10, 5, 2, 7, 8, 7] and k = 3, we should get: [10, 7, 8, 8], since:
10 = max(10, 5, 2)
7 = max(5, 2, 7)
8 = max(2, 7, 8)
8 = max(7, 8, 7)
Do this in O(n) time and O(k) space. You can modify the input array in-place and you do not need to store the results. You can simply print them out as you compute them.
What does O(n) time and O(k) space mean?
I got this solution. Does this fulfill the requirements? If not, why not?
def foo(arr, k):
for idx in range(0, len(arr) - k + 1):
maxI = -1
for i in arr[idx:idx + k]:
if i > maxI:
maxI = i
print(maxI) #3; 3; 3; 5; 5; 5; 10; 22
arr = [0, 1, 3, 1, 2, 5, 5, 4, 10, 22]
k = 3
foo(arr, k)