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.
vararr=[];varmax=0;// init each element of arr to 0for(letl=0;l<n;l++){arr[l]=0;}// for each sum operation in queriesfor(leti=0;i<queries.length;i++){// update arr with number to add at index=queries[i][0] and number to remove at index=queries[i][0]+1 => this will allow us to build each element of the final array by summing all elements before it. The aim of this trick is to lower time complexityarr[queries[i][0]-1]+=queries[i][2];if(queries[i][1]<arr.length){arr[queries[i][1]]-=queries[i][2];}}for(letj=1;j<n;j++){arr[j]+=arr[j-1];}for(letk=0;k<arr.length;k++){max=Math.max(max,arr[k]);}//max = Math.max(...arr); // not working for big arraysreturnmax;
Hey,
I did the code in Java8 and my code is getting failed for input type - where only single value is present in a row of array. meaning only left index value is provided and right and k value is missing from array.
So can you help me how to solve this issue?
but it would be great if you can please provide your comments, like, dislike on my video how i did it..It motivates me to create better content for my audience.
I appologize for my bad sound quality but i am trying to improve it.
but it will be very difficult to add subtitle for this video because its around half an hour which explains all the concepts in deep.
Making this long video took lot of effort and time now adding a subtitle will be very tedious without any support.
Will suggest you to try automatic transcribe feature from youtube to translate it.
I had the same issue. my mistake was decrementing lower and upper. you don't decrement upper, the difference array needs to show it went down AFTER then last index, not within.
You could simplify your code a smidge, and save a little processing power, by removing your final for loop, and putting in an if check in the second to last loop, like this:
My js solution ran out of time for a few test cases
function arrayManipulation(n, queries) {
let a = new Array(n).fill(0);
for(let j = 0; j < queries.length; j++) {
let temp = queries[j]
let k = temp[0];
while(k <= temp[1]) {
a[k-1] += temp[2];
k++;
}
Array Manipulation
You are viewing a single comment's thread. Return to all comments →
Same solution in Javascript
Hey, I did the code in Java8 and my code is getting failed for input type - where only single value is present in a row of array. meaning only left index value is provided and right and k value is missing from array. So can you help me how to solve this issue?
you could post your code,and we can check it out
simpler in es6:
function arrayManipulation(n, queries) { let arr = new Array(2*n).fill(0); let max = 0;
}
I did something pretty similar, just with a little bit more readable forEach:
This produces wrong answer in some of the tests.
Hi,
try this. Here is the video tutorial for my solution O(n+m) complexity.
https://www.youtube.com/watch?v=hDhf04AJIRs&list=PLSIpQf0NbcCltzNFrOJkQ4J4AAjW3TSmA
Would really appreciate your feedback like and comment etc. on my video.
It is a good video. I understood the algorithm clearly.
thanks @chandraprabha90.
but it would be great if you can please provide your comments, like, dislike on my video how i did it..It motivates me to create better content for my audience.
Could you add subtitles? I tried watching it but couldn't quite understand your accent through the audio
Hi Grozny,
I appologize for my bad sound quality but i am trying to improve it. but it will be very difficult to add subtitle for this video because its around half an hour which explains all the concepts in deep.
Making this long video took lot of effort and time now adding a subtitle will be very tedious without any support.
Will suggest you to try automatic transcribe feature from youtube to translate it.
Anyway thanks for watching.
Hi Grozny,
I have added subtitle for this tutorial and I hope it will you to understand logic with more clarity.
Nice approach you got there!
I had the same issue. my mistake was decrementing lower and upper. you don't decrement upper, the difference array needs to show it went down AFTER then last index, not within.
Thought to myself ....no js solutions here? then I find 3 of them, Js, ES6, readable wow.
Your last for loop isn't needed. You can move Math.max to the previous for loop.
Added some ES6 syntax suger...
Got stuck because of this
Thanks man!
You could simplify your code a smidge, and save a little processing power, by removing your final for loop, and putting in an if check in the second to last loop, like this:
Perfect! Could you please explain me the thought process behind the solution?
My js solution ran out of time for a few test cases
function arrayManipulation(n, queries) { let a = new Array(n).fill(0); for(let j = 0; j < queries.length; j++) { let temp = queries[j] let k = temp[0]; while(k <= temp[1]) { a[k-1] += temp[2]; k++; }
} return Math.max.apply(Math, a); }