Range Addition
也是陈年老题了,关键在于没想到结束位置应该用负数,其实再仔细一想,这个技巧就是扫描线原理而已
class Solution(object):
def getModifiedArray(self, length, updates):
"""
:type length: int
:type updates: List[List[int]]
:rtype: List[int]
"""
result = [0] * length
for start, end, value in updates:
result[start] += value
if end + 1 < length:
result[end + 1] -= value
for i in xrange(1, length):
result[i] += result[i - 1]
return result