Skip to content

Instantly share code, notes, and snippets.

@Gosilama
Created September 24, 2020 22:52
Show Gist options
  • Save Gosilama/0ad10d4341927a2e48151b1514beaeb3 to your computer and use it in GitHub Desktop.
Save Gosilama/0ad10d4341927a2e48151b1514beaeb3 to your computer and use it in GitHub Desktop.
const arrayManipulation = (n, queries) => {
const zeroArr = [];
let max = 0;
for (let i = 0; i < n; i++) {
zeroArr.push(0);
}
queries.forEach((query) => {
zeroArr[query[0] - 1] += query[2];
if (query[1] < n) {
zeroArr[query[1]] -= query[2];
}
});
for (let i = 1; i < n; i++) {
zeroArr[i] += zeroArr[i - 1];
}
zeroArr.forEach(value => {
max = max > value ? max : value;
});
return max;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment