Skip to content

Instantly share code, notes, and snippets.

@nhnam
Created January 8, 2022 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nhnam/73b91379a0b56462915c4f268ebe992a to your computer and use it in GitHub Desktop.
Save nhnam/73b91379a0b56462915c4f268ebe992a to your computer and use it in GitHub Desktop.
function arrayManipulation(n, queries) {
// Write your code here
var arr = Array(n + 1).fill(0);
var max = 0;
var x = 0;
for(var query of queries) {
const [a, b, k] = query;
arr[a] += k;
if(b+1 <= n) {
arr[b+1] -= k;
}
}
for(var i = 1; i <= n; i++) {
x = x + arr[i];
if(x > max) {
max = x;
}
}
return max;
}
const queries = [[1, 2, 100], [2, 5, 100], [3, 4, 100]];
let res = arrayManipulation(5, queries);
res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment