• + 0 comments

    slightly more understandable variation of the solution (in python)

    arr = [0] * (n + 1)
    
    for query in queries:
        # where a represents the lower bound of the range,
        # and b represents the upper bound.
        a, b, k = query
        arr[a] += k
        try:
            arr[b+1] -= k
        except IndexError:
            pass
    
    max_val = running_sum = 0
    
    for delta in arr:
        # delta can be either positive or negative,
        # but in any case it is added to the running sum.
        # however max_val always contains the greatest sum so far
        running_sum += delta
        if max_val < running_sum:
            max_val = running_sum
    
    return max_val