-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathrange-addition.py
39 lines (28 loc) · 1.12 KB
/
range-addition.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from typing import List
class Solution:
def getModifiedArray(self, length: int, updates: List[List[int]]) -> List[int]:
result = [0] * length
range_cache = [0] * length
for begin, end, value in updates:
range_cache[0 if begin < 0 else begin] += value
if end + 1 < length:
range_cache[end + 1] -= value
value = 0
for pos in range(length):
value += range_cache[pos]
result[pos] = value
return result
def getModifiedArray1(self, length: int, updates: List[List[int]]) -> List[int]:
stack_begin = list(sorted(updates, key=lambda x: -x[0]))
stack_end = list(sorted(updates, key=lambda x: -x[1]))
value = 0
result = [0] * length
for pos in range(length):
while stack_begin and stack_begin[-1][0] <= pos:
value += stack_begin[-1][2]
stack_begin.pop()
while stack_end and stack_end[-1][1] < pos:
value -= stack_end[-1][2]
stack_end.pop()
result[pos] = value
return result