You are given a list of size N, initialized with zeroes. You have to perform M operations on the list and output the maximum of final values of all the N elements in the list. For every operation, you are given three integers a,b and k and you have to add value to all the elements ranging from index to (both inclusive).
Input Format
First line will contain two integers N and M separated by a single space. Next M lines will contain three integers a,b and k separated by a single space. Numbers in list are numbered from 1 to N .
Constraints
Click here
Output Format
A single line containing maximum value in the updated list.
Sample Input
5 3
1 2 100
2 5 100
3 4 100
Sample Output
200
Explanation
After first update list will be 100 100 0 0 0.
After second update list will be 100 200 100 100 100.
After third update list will be 100 200 200 200 100.
So the required answer will be 200.
One of the Solutions with less time complexity
n, inputs = [int(n) for n in input().split(" ")]
list = [0]*(n+1)
for _ in range(inputs):
x, y, incr = [int(n) for n in input().split(" ")]
list[x-1] += incr
if((y)<=len(list)):
list[y] -= incr
max = x = 0
for i in list:
x=x+i;
if(max<x):max=x
print(max)
Can someone explain the above solution?