Queries with Fixed Length

  • + 2 comments

    Python solution that passes all test cases. I am only looking for the bounds of each subquery so I'm saving a lot of time.

    def solve(arr, queries):
      n = len(arr)
      minimums = []
      for d in queries:
        maximum = minimum = max(arr[0:d])
        for i in range(1, n - d + 1):
          # New upper bound is higher
          if arr[i + d - 1] > maximum:
            maximum = arr[i + d - 1]
          # Previous lower bound was the maximum
          elif arr[i - 1] == maximum:
            maximum = max(arr[i : i + d])
          # Else, we mantain the current maximum
          
          # We update the minimum
          if maximum < minimum:
            minimum = maximum
        minimums.append(minimum)
      return minimums