Range Addition

Problem: Range Addition

We can only add INC to the begin of the update and remove INC at the end of the update. When we want to get the result, we can use accumulation to get the result.

Code in Python:

class Solution(object):
    def getModifiedArray(self, length, updates):
        """
        :type length: int
        :type updates: List[List[int]]
        :rtype: List[int]
        """
        res = [0] * length
        for start, end, inc in updates:
            res[start] += inc
            if end+1 < length: res[end+1] -= inc

        for i in xrange(1, len(res)):
            res[i] += res[i-1]

        return res

results matching ""

    No results matching ""