We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

process.stdin.resume();process.stdin.setEncoding('ascii');varinput_stdin="";varinput_stdin_array="";varinput_currentline=0;process.stdin.on('data',function(data){input_stdin+=data;});process.stdin.on('end',function(){input_stdin_array=input_stdin.split("\n");main();});functionreadLine(){returninput_stdin_array[input_currentline++];}/////////////// ignore above this line ////////////////////functionmain(){varn_temp=readLine().split(' ');varlistSize=parseInt(n_temp[0]);varlineNumber=parseInt(n_temp[1]);letslopList=newArray();for(leti=0;i<listSize;i++){slopList.push(0);}for(vara0=0;a0<lineNumber;a0++){vara_temp=readLine().split(' ');constbeginElementPos=parseInt(a_temp[0]);constendElementPos=parseInt(a_temp[1]);constaddValue=parseInt(a_temp[2]);slopList[beginElementPos-1]+=addValue;if(endElementPos<listSize){slopList[endElementPos]-=addValue;}}letactualList=newArray();letsum=0;for(leti=0;i<listSize;i++){sum+=slopList[i];actualList.push(sum);}letmax=actualList.reduce((acc,val)=>{return(acc>val)?acc:val;},0);console.log(max);}

## Array Manipulation

You are viewing a single comment's thread. Return to all comments →

Guys, if you look for a clear understanding of the solution, I read a pretty clear comment down the road that clarified my mind.

Basically, when you add value from a to b you just need to know that it goes up from k and goes down of k after.

What this algo does is to register the slopes only, so we just need 2 entry, with O(1) complexity.

We just need to know that we are upping from k at the beginning and decreasing at the end.

Finally, the maximum would be...

The addition of all the slopes, that is why it's max(sum) of the tables, because the tables itself registers the slopes

Thanks a lot!!That really helped!

Can you explain the concept of just adding add subtracting at a particular index? I mean how have we arrived to this logic?

Hi,

I have uploaded a video tutorial which can help you to understand the problem as well as logic.

Here is the video tutorial for my solution O(n+m) complexity.

https://youtu.be/hDhf04AJIRs

Would really appreciate your feedback like, dislike , comment etc. on my video.

Hi,

Here is the video tutorial for my solution O(n+m) complexity.

https://youtu.be/hDhf04AJIRs

Would really appreciate your feedback like and comment etc. on my video.

I've add it to my playlist

haha..thank you.

but if you dont mind, Would really appreciate your feedback like, dislike , comment etc. on my video.

I didnt well understand that what will happen if b+1 is out of array bounds?

it can't be out of bounds, it saids that b>n in the problem statement.

if b+1 > n then the the addition of k from position a will continue till the last element of the array.

Hi,

I have uploaded a video tutorial which can help you to understand the problem as well as logic.

Here is the video tutorial for my solution O(n+m) complexity.

https://youtu.be/hDhf04AJIRs

Would really appreciate your feedback like, dislike , comment etc. on my video.

Jesus christ, it all makes sense now after that graph lol, I kept wondering what drug these people were taking to arrive at this conclusion.

can you explain ? :)

Very well explained (Y)