Queries with Fixed Length

  • + 1 comment
    from collections import deque
    def solve1(arr,dis):
        ans=[];d=deque();i=0;j=0
        while j<len(arr):
            while d and d[-1]<arr[j]:
                d.pop()
            d.append(arr[j])
            if j-i+1<dis:
                j+=1
            else:
                ans.append(d[0])
                if d[0]==arr[i]:
                    d.popleft()
                i+=1
                j+=1
        return min(ans)
        
    
    def solve(arr, queries):
        res=[]
        for i in queries:
            if i==1:
                res.append(min(arr))
            elif i==len(arr):
                res.append(max(arr))
            else:
                res.append(solve1(arr,i))
        return res