• + 0 comments

    Thank you. This is clear.

    You could also find the max using: max(accumulate(my_array))

    See below:

    def arrayManipulation(n, queries):
        # initialise list
        arr = [0] * (n + 1)
    
        # update
        for a, b, k in queries:
            arr[a - 1] += k
            arr[b] -= k
        
        # accumulate values
        return max(accumulate(arr))
    

    However, I have not understood the theory behind the difference array algorithm, why it works the way it works and how to derive it.