• + 0 comments

    Would it possible to optimise this, many testcase failing due to time complexity, as its time complexity is O(n^3)

    def solve(a, queries):
        res = []
        for q in queries:
            sum = 0
            for i in range(q[0]-1,q[1]):
                sum += a[i]
                for j in range(i+1,q[1]):
                    sum += max(a[i:j+1])
            res.append(sum)
        return res