Given an array and an integer A, find the maximum for each contiguous subarray of size A.
Below is a more detailed walkthrough of what you should be trying to code, using the example above:
# Data
= [1, 2, 3, 1, 4, 5, 2, 3, 6]
array = 3
a
# List comprehension: upper limit excluded
max(array[i:i + a]) for i in range(0, len(array) - a + 1)] [
## [3, 3, 4, 5, 5, 5, 6]
def max_subarray(array, a):
return [max(array[i:i + a]) for i in range(0, len(array) - a + 1)]
= [2, 4, 6, 3, 10, 7]
array_1 = 3
a
max_subarray(array_1, a)
## [6, 6, 10, 10]
= [1, 5, 7, 8, 4, 8, 3, 10, 15, 67, 8, 0, 1]
array_2 = 4
a
max_subarray(array_2, a)
## [8, 8, 8, 8, 10, 15, 67, 67, 67, 67]