# Solutions for Max Min

• @Charles, submitted in Python 2, achived 100% score
``````from collections import defaultdict
import json
import math

def solve(arr):
stack = []
arr.append(0)
d=defaultdict(int)
for i,j in enumerate(arr):
t=i
while stack and stack[-1][0]>=j:
val,li = stack.pop()
d[j]=max(d[j],i-li+1)
d[val]=max(d[val],i-li)
t=li
stack.append((j,t))

del d[0]
e=defaultdict(int)
for i in d:
e[d[i]]=max(e[d[i]],i)

l=len(arr)
ans=[e[l-1]]
for i in range(len(arr)-2,0,-1):
if e[i]<ans[-1]:
ans.append(ans[-1])
else:
ans.append(e[i])
return ans[::-1]

``````
• @william, submitted in Python 3, achived 100% score
``````from collections import defaultdict
def solve(arr):
stack = []
arr.append(0)
d=defaultdict(int)
for i,j in enumerate(arr):
t=i
while stack and stack[-1][0]>=j:
val,li = stack.pop()
d[j]=max(d[j],i-li+1)
d[val]=max(d[val],i-li)
t=li
stack.append((j,t))

del d[0]
e=defaultdict(int)
for i in d:
e[d[i]]=max(e[d[i]],i)

l=len(arr)
ans=[e[l-1]]
for i in range(len(arr)-2,0,-1):
if e[i]<ans[-1]:
ans.append(ans[-1])
else:
ans.append(e[i])
return ans[::-1]``````