I am doing this Array Manipulation problem from hackerrank and it tells me runtime error. From my perspective, the time complexity is O(n). But it still got this problem. Anyone could help me out?
Below is the link:
Starting with a 1-indexed array of zeros and a list of operations, for each operation add a value to each of the array element between two given indices, inclusive. Once all operations have been performed, return the maximum value in your array.
For example, the length of your array of zeros . Your list of queries is as follows:
a b k 1 5 3 4 8 7 6 9 1Add the values of between the indices and inclusive:
index -> 1 2 3 4 5 6 7 8 9 10 [0,0,0, 0, 0,0,0,0,0, 0] [3,3,3, 3, 3,0,0,0,0, 0] [3,3,3,10,10,7,7,7,0, 0] [3,3,3,10,10,8,8,8,1, 0]The largest value is after all operations are performed.
I attached my code here:
def arrayManipulation(n,queries):
increment=[]
value=[]
for i in range(n):
increment+=[0]
value+=[0]
for j in range(m):
left=queries[j][0]
right=queries[j][1]
k=queries[j][2]
increment[left-1]+=k
if right>=n:
continue
else:
increment[right]-=k
value[0]=increment[0]
for i in range(1,n):
value[i]=value[i-1]+increment[i]
maximum=max(value)
return maximum
