The problem is, that you create an array of length n for each operation (in the input array), and then sum them by calling reduce on the result and after that you finally find the maximum of that resulting array. But you only need one initial array to sum up in, and you can find the maximum in the same operation - here in a rather verbose form:
function arrayManipulation(n, arr) {
var res = [];
var max = 0;
for (var i = 0; i < arr.length; i++) {
for (var j = 0; j < n; j++) {
if (i === 0) {
res.push(j >= arr[i][0] - 1 && j <= arr[i][1] - 1 ? arr[i][2] : 0);
}
else {
res[j] += j >= arr[i][0] && j <= arr[i][1] ? arr[i][2] : 0;
}
max = Math.max(max, res[j]);
}
}
return max;
}
which can be narrowed down to this more succinct form:
function arrayManipulation(n, arr) {
var res = [];
var max = Number.MIN_VALUE;
for (var i = 0; i < arr.length; i++) {
for (var j = arr[i][0]; j <= arr[i][1]; j++) {
res[j] = (res[j] || 0) + arr[i][2];
max = Math.max(max, res[j]);
}
}
return max;
}
Here is used, that it is only needed to loop through the intervals of the array that is actually manipulated by each operation in the input array. So for instance in the example operations, it is for the first operation only necessary to add 3 to the values in places from 1 to 5 inclusive.