Queries with Fixed Length

  • + 0 comments

    Monotonic two-sided queue in Python 3 (accepted):

    def solve(arr, queries):
        # Write your code here
        def get_val(arr, d):
            max_record = deque()
            ans = float('inf')
            for i in range(n):
                while max_record and arr[max_record[-1]] <= arr[i]:
                    max_record.pop()
                max_record.append(i)
                if max_record[0] <= i-d:
                    max_record.popleft()
                if i >= d-1:
                    ans = min(ans, arr[max_record[0]])  
            return ans 
        return [get_val(arr, q) for q in queries]