I have an array of integers, I have to find all sub-arrays of different size from 1 to len(array). In every sub-array, I have to find the minimum value in it, once done I have to find the maximum value between the minimum values of all the subarrays of the same size. Finally, I have to return these maximum values in a list.
So the first value in my list will always be the maximum integer in the array, the second value will be the maximum between the minimum values of the sub-arrays size 2, and so on; the last value in the list will always be the minimum integer of the array. Original problem here: Min Max Riddle
def min_max_subarrays(arr):
result = [max(arr), ]
w = 2
while w < len(arr):
temp = 0
for i, n in enumerate(arr[:-w+1]):
x = min(arr[i:i+w])
if x > temp:
temp = x
result.append(temp)
w += 1
result.append(min(arr))
return result
The function is correct, but I'm sure there's a way to reduce time complexity. I feel one loop is unnecessary, but I'm struggling to find a way to remove it.