Queries with Fixed Length

  • + 0 comments

    Python DP ?, O(N*Q)

    def solve(arr, queries):
        def solve2(batches, d):
            a = [x for ba in batches for x in accumulate(reversed(ba), max)]
            b = [x for ba in batches for x in accumulate(ba, max)]
            return min(max(a[i-2*((i+1)%d)], b[i]) for i in range(d-1, n))
            
        return [solve2(list(batched(arr, d)), d) for d in queries]