-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy patharray-manipulation.py
More file actions
54 lines (42 loc) · 1.26 KB
/
Copy patharray-manipulation.py
File metadata and controls
54 lines (42 loc) · 1.26 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/python3
import math
import os
import random
import re
import sys
def arrayManipulation(n, queries):
diffs = getArrayOfDiffs(n, queries)
return maxFromDiffs(diffs)
def maxFromDiffs(diffs):
sumSoFar = 0
maxSoFar = 0
for diff in diffs:
sumSoFar += diff
if sumSoFar > maxSoFar:
maxSoFar = sumSoFar
return maxSoFar
def getArrayOfDiffs(n, queries):
# An array used to capture the difference of an element
# compared to the previous element.
# Therefore the value of diffs[n] after all array manipulations is
# the cumulative sum of values from diffs[0] to diffs[n - 1]
diffs = [0] * n
for a, b, k in queries:
# Adds "k" to all subsequent elements in the array
diffs[a - 1] += k
# Ignore if b is out of range
if (b < n):
# Subtracts "k" from all subsequent elements in the array
diffs[b] -= k
return diffs
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
nm = input().split()
n = int(nm[0])
m = int(nm[1])
queries = []
for _ in range(m):
queries.append(list(map(int, input().rstrip().split())))
result = arrayManipulation(n, queries)
fptr.write(str(result) + '\n')
fptr.close()