-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcruch_optimized.js
More file actions
32 lines (25 loc) · 729 Bytes
/
cruch_optimized.js
File metadata and controls
32 lines (25 loc) · 729 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/arrays/crush.md]]
*/
function arrayManipulation(n, queries) {
// why adding 2?
// first slot to adjust 1-based index and
// last slot for storing accumSum result
const LENGTH = n + 2;
const INITIAL_VALUE = 0;
const result = Array(LENGTH).fill(INITIAL_VALUE);
let maximum = 0;
queries.forEach((query) => {
const [aStart, bEnd, kValue] = query;
result[aStart] += kValue;
result[bEnd + 1] -= kValue;
});
let accumSum = 0;
result.forEach((value) => {
accumSum += value;
maximum = Math.max(maximum, accumSum);
});
return maximum;
}
export default { arrayManipulation };
export { arrayManipulation };