Skip to content

Commit 4ff7230

Browse files
committed
Time: 7 ms (55.49%), Space: 23.5 MB (48.3%) - LeetHub
1 parent 5cf68a7 commit 4ff7230

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# time complexity: O(n)
2+
# space complexity: O(n)
3+
from typing import List
4+
5+
6+
class Solution:
7+
def getModifiedArray(self, length: int, updates: List[List[int]]) -> List[int]:
8+
result = [0] * (length + 1)
9+
for update in updates:
10+
result[update[0]] += update[2]
11+
result[update[1]+1] -= update[2]
12+
sum = 0
13+
for i, num in enumerate(result):
14+
sum += num
15+
result[i] = sum
16+
return result[:-1]
17+
18+
19+
length = 5
20+
updates = [[1, 3, 2], [2, 4, 3], [0, 2, -2]]
21+
print(Solution().getModifiedArray(length, updates))
22+
length = 10
23+
updates = [[2, 4, 6], [5, 6, 8], [1, 9, -4]]
24+
print(Solution().getModifiedArray(length, updates))

0 commit comments

Comments
 (0)