[
accumulate
array
]
BinarySearch 0356 Range Update
Problem statement
https://binarysearch.com/problems/Range-Update/
Solution
Use difference trick, then each operation can be done in O(1)
and in the end use accumulate.
Complexity
It is O(n + m)
, where n = len(nums)
and m = len(o)
for time and O(n)
for space.
Code
class Solution:
def solve(self, nums, o):
diff = nums[0:1] + [y - x for x, y in zip(nums, nums[1:])] + [0]
for l, r, x in o:
diff[l] += x
diff[r + 1] -= x
return list(accumulate(diff))[:-1]