Original case: [0,0,0,0,0]
Change: (1, 3, 2)
After: [0,2,2,2,0]
Diff: [0,2,0,0,-2]
Explaination: One thing that we don't need to care about which is the differences between the start_index and end_index, since they are paralleled to increment. However, we do need to care the differences between start_index - 1 and start_index; end_index and end_index + 1, since numbers list[:start_index] and list[end_index + 1:] will not be changed.
class Solution:
def getModifiedArray(self, length: int, updates: List[List[int]]) -> List[int]:
diff = [0 for i in range(length + 1)]
for start_index, end_index, value in updates:
diff[start_index] += value
diff[end_index + 1] -= value
for i in range(1, len(diff)):
diff[i] = diff[i - 1] + diff[i]
return diff[:-1]